gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / i2c / busses / i2c-cros-ec-tunnel.c
blob790ea3fda693b453cc58bbee02c2b6fc489d49f1
1 // SPDX-License-Identifier: GPL-2.0+
2 // Expose an I2C passthrough to the ChromeOS EC.
3 //
4 // Copyright (C) 2013 Google, Inc.
6 #include <linux/acpi.h>
7 #include <linux/module.h>
8 #include <linux/i2c.h>
9 #include <linux/platform_data/cros_ec_commands.h>
10 #include <linux/platform_data/cros_ec_proto.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
14 #define I2C_MAX_RETRIES 3
16 /**
17 * struct ec_i2c_device - Driver data for I2C tunnel
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.
27 struct ec_i2c_device {
28 struct device *dev;
29 struct i2c_adapter adap;
30 struct cros_ec_device *ec;
32 u16 remote_bus;
34 u8 request_buf[256];
35 u8 response_buf[256];
38 /**
39 * ec_i2c_count_message - Count bytes needed for ec_i2c_construct_message
41 * @i2c_msgs: The i2c messages to read
42 * @num: The number of i2c messages.
44 * Returns the number of bytes the messages will take up.
46 static int ec_i2c_count_message(const struct i2c_msg i2c_msgs[], int num)
48 int i;
49 int size;
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;
57 return size;
60 /**
61 * ec_i2c_construct_message - construct a message to go to the EC
63 * This function effectively stuffs the standard i2c_msg format of Linux into
64 * a format that the EC understands.
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.
71 * Returns 0 or a negative error number.
73 static int ec_i2c_construct_message(u8 *buf, const struct i2c_msg i2c_msgs[],
74 int num, u16 bus_num)
76 struct ec_params_i2c_passthru *params;
77 u8 *out_data;
78 int i;
80 out_data = buf + sizeof(struct ec_params_i2c_passthru) +
81 num * sizeof(struct ec_params_i2c_passthru_msg);
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 = &params->msg[i];
90 msg->len = i2c_msg->len;
91 msg->addr_flags = i2c_msg->addr;
93 if (i2c_msg->flags & I2C_M_TEN)
94 return -EINVAL;
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;
104 return 0;
108 * ec_i2c_count_response - Count bytes needed for ec_i2c_parse_response
110 * @i2c_msgs: The i2c messages to to fill up.
111 * @num: The number of i2c messages expected.
113 * Returns the number of response bytes expeced.
115 static int ec_i2c_count_response(struct i2c_msg i2c_msgs[], int num)
117 int size;
118 int i;
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;
125 return size;
129 * ec_i2c_parse_response - Parse a response from the EC
131 * We'll take the EC's response and copy it back into msgs.
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.
138 * Returns 0 or a negative error number.
140 static int ec_i2c_parse_response(const u8 *buf, struct i2c_msg i2c_msgs[],
141 int *num)
143 const struct ec_response_i2c_passthru *resp;
144 const u8 *in_data;
145 int i;
147 in_data = buf + sizeof(struct ec_response_i2c_passthru);
149 resp = (const struct ec_response_i2c_passthru *)buf;
150 if (resp->i2c_status & EC_I2C_STATUS_TIMEOUT)
151 return -ETIMEDOUT;
152 else if (resp->i2c_status & EC_I2C_STATUS_NAK)
153 return -ENXIO;
154 else if (resp->i2c_status & EC_I2C_STATUS_ERROR)
155 return -EIO;
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;
162 for (i = 0; i < *num; i++) {
163 struct i2c_msg *i2c_msg = &i2c_msgs[i];
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;
171 return 0;
174 static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
175 int num)
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;
182 int alloc_size;
183 int result;
184 struct cros_ec_command *msg;
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);
189 return request_len;
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);
196 return response_len;
199 alloc_size = max(request_len, response_len);
200 msg = kmalloc(sizeof(*msg) + alloc_size, GFP_KERNEL);
201 if (!msg)
202 return -ENOMEM;
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;
210 msg->version = 0;
211 msg->command = EC_CMD_I2C_PASSTHRU;
212 msg->outsize = request_len;
213 msg->insize = response_len;
215 result = cros_ec_cmd_xfer_status(bus->ec, msg);
216 if (result < 0) {
217 dev_err(dev, "Error transferring EC i2c message %d\n", result);
218 goto exit;
221 result = ec_i2c_parse_response(msg->data, i2c_msgs, &num);
222 if (result < 0)
223 goto exit;
225 /* Indicate success by saying how many messages were sent */
226 result = num;
227 exit:
228 kfree(msg);
229 return result;
232 static u32 ec_i2c_functionality(struct i2c_adapter *adap)
234 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
237 static const struct i2c_algorithm ec_i2c_algorithm = {
238 .master_xfer = ec_i2c_xfer,
239 .functionality = ec_i2c_functionality,
242 static int ec_i2c_probe(struct platform_device *pdev)
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;
250 if (!ec->cmd_xfer) {
251 dev_err(dev, "Missing sendrecv\n");
252 return -EINVAL;
255 bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
256 if (bus == NULL)
257 return -ENOMEM;
259 err = device_property_read_u32(dev, "google,remote-bus", &remote_bus);
260 if (err) {
261 dev_err(dev, "Couldn't read remote-bus property\n");
262 return err;
264 bus->remote_bus = remote_bus;
266 bus->ec = ec;
267 bus->dev = dev;
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;
274 bus->adap.dev.of_node = pdev->dev.of_node;
275 bus->adap.retries = I2C_MAX_RETRIES;
276 ACPI_COMPANION_SET(&bus->adap.dev, ACPI_COMPANION(&pdev->dev));
278 err = i2c_add_adapter(&bus->adap);
279 if (err)
280 return err;
281 platform_set_drvdata(pdev, bus);
283 return err;
286 static int ec_i2c_remove(struct platform_device *dev)
288 struct ec_i2c_device *bus = platform_get_drvdata(dev);
290 i2c_del_adapter(&bus->adap);
292 return 0;
295 static const struct of_device_id cros_ec_i2c_of_match[] = {
296 { .compatible = "google,cros-ec-i2c-tunnel" },
299 MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
301 static const struct acpi_device_id cros_ec_i2c_tunnel_acpi_id[] = {
302 { "GOOG0012", 0 },
305 MODULE_DEVICE_TABLE(acpi, cros_ec_i2c_tunnel_acpi_id);
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",
312 .acpi_match_table = ACPI_PTR(cros_ec_i2c_tunnel_acpi_id),
313 .of_match_table = of_match_ptr(cros_ec_i2c_of_match),
317 module_platform_driver(ec_i2c_tunnel_driver);
319 MODULE_LICENSE("GPL");
320 MODULE_DESCRIPTION("EC I2C tunnel driver");
321 MODULE_ALIAS("platform:cros-ec-i2c-tunnel");