Merge tag 'modules-next-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / i2c / busses / i2c-cros-ec-tunnel.c
1 /*
2  *  Copyright (C) 2013 Google, Inc
3  *
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.
8  *
9  * Expose an I2C passthrough to the ChromeOS EC.
10  */
11
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>
18
19 #define I2C_MAX_RETRIES 3
20
21 /**
22  * struct ec_i2c_device - Driver data for I2C tunnel
23  *
24  * @dev: Device node
25  * @adap: I2C adapter
26  * @ec: Pointer to EC device
27  * @remote_bus: The EC bus number we tunnel to on the other side.
28  * @request_buf: Buffer for transmitting data; we expect most transfers to fit.
29  * @response_buf: Buffer for receiving data; we expect most transfers to fit.
30  */
31
32 struct ec_i2c_device {
33         struct device *dev;
34         struct i2c_adapter adap;
35         struct cros_ec_device *ec;
36
37         u16 remote_bus;
38
39         u8 request_buf[256];
40         u8 response_buf[256];
41 };
42
43 /**
44  * ec_i2c_count_message - Count bytes needed for ec_i2c_construct_message
45  *
46  * @i2c_msgs: The i2c messages to read
47  * @num: The number of i2c messages.
48  *
49  * Returns the number of bytes the messages will take up.
50  */
51 static int ec_i2c_count_message(const struct i2c_msg i2c_msgs[], int num)
52 {
53         int i;
54         int size;
55
56         size = sizeof(struct ec_params_i2c_passthru);
57         size += num * sizeof(struct ec_params_i2c_passthru_msg);
58         for (i = 0; i < num; i++)
59                 if (!(i2c_msgs[i].flags & I2C_M_RD))
60                         size += i2c_msgs[i].len;
61
62         return size;
63 }
64
65 /**
66  * ec_i2c_construct_message - construct a message to go to the EC
67  *
68  * This function effectively stuffs the standard i2c_msg format of Linux into
69  * a format that the EC understands.
70  *
71  * @buf: The buffer to fill.  We assume that the buffer is big enough.
72  * @i2c_msgs: The i2c messages to read.
73  * @num: The number of i2c messages.
74  * @bus_num: The remote bus number we want to talk to.
75  *
76  * Returns 0 or a negative error number.
77  */
78 static int ec_i2c_construct_message(u8 *buf, const struct i2c_msg i2c_msgs[],
79                                     int num, u16 bus_num)
80 {
81         struct ec_params_i2c_passthru *params;
82         u8 *out_data;
83         int i;
84
85         out_data = buf + sizeof(struct ec_params_i2c_passthru) +
86                    num * sizeof(struct ec_params_i2c_passthru_msg);
87
88         params = (struct ec_params_i2c_passthru *)buf;
89         params->port = bus_num;
90         params->num_msgs = num;
91         for (i = 0; i < num; i++) {
92                 const struct i2c_msg *i2c_msg = &i2c_msgs[i];
93                 struct ec_params_i2c_passthru_msg *msg = &params->msg[i];
94
95                 msg->len = i2c_msg->len;
96                 msg->addr_flags = i2c_msg->addr;
97
98                 if (i2c_msg->flags & I2C_M_TEN)
99                         msg->addr_flags |= EC_I2C_FLAG_10BIT;
100
101                 if (i2c_msg->flags & I2C_M_RD) {
102                         msg->addr_flags |= EC_I2C_FLAG_READ;
103                 } else {
104                         memcpy(out_data, i2c_msg->buf, msg->len);
105                         out_data += msg->len;
106                 }
107         }
108
109         return 0;
110 }
111
112 /**
113  * ec_i2c_count_response - Count bytes needed for ec_i2c_parse_response
114  *
115  * @i2c_msgs: The i2c messages to to fill up.
116  * @num: The number of i2c messages expected.
117  *
118  * Returns the number of response bytes expeced.
119  */
120 static int ec_i2c_count_response(struct i2c_msg i2c_msgs[], int num)
121 {
122         int size;
123         int i;
124
125         size = sizeof(struct ec_response_i2c_passthru);
126         for (i = 0; i < num; i++)
127                 if (i2c_msgs[i].flags & I2C_M_RD)
128                         size += i2c_msgs[i].len;
129
130         return size;
131 }
132
133 /**
134  * ec_i2c_parse_response - Parse a response from the EC
135  *
136  * We'll take the EC's response and copy it back into msgs.
137  *
138  * @buf: The buffer to parse.
139  * @i2c_msgs: The i2c messages to to fill up.
140  * @num: The number of i2c messages; will be modified to include the actual
141  *       number received.
142  *
143  * Returns 0 or a negative error number.
144  */
145 static int ec_i2c_parse_response(const u8 *buf, struct i2c_msg i2c_msgs[],
146                                  int *num)
147 {
148         const struct ec_response_i2c_passthru *resp;
149         const u8 *in_data;
150         int i;
151
152         in_data = buf + sizeof(struct ec_response_i2c_passthru);
153
154         resp = (const struct ec_response_i2c_passthru *)buf;
155         if (resp->i2c_status & EC_I2C_STATUS_TIMEOUT)
156                 return -ETIMEDOUT;
157         else if (resp->i2c_status & EC_I2C_STATUS_ERROR)
158                 return -EREMOTEIO;
159
160         /* Other side could send us back fewer messages, but not more */
161         if (resp->num_msgs > *num)
162                 return -EPROTO;
163         *num = resp->num_msgs;
164
165         for (i = 0; i < *num; i++) {
166                 struct i2c_msg *i2c_msg = &i2c_msgs[i];
167
168                 if (i2c_msgs[i].flags & I2C_M_RD) {
169                         memcpy(i2c_msg->buf, in_data, i2c_msg->len);
170                         in_data += i2c_msg->len;
171                 }
172         }
173
174         return 0;
175 }
176
177 static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
178                        int num)
179 {
180         struct ec_i2c_device *bus = adap->algo_data;
181         struct device *dev = bus->dev;
182         const u16 bus_num = bus->remote_bus;
183         int request_len;
184         int response_len;
185         u8 *request = NULL;
186         u8 *response = NULL;
187         int result;
188         struct cros_ec_command msg;
189
190         request_len = ec_i2c_count_message(i2c_msgs, num);
191         if (request_len < 0) {
192                 dev_warn(dev, "Error constructing message %d\n", request_len);
193                 result = request_len;
194                 goto exit;
195         }
196         response_len = ec_i2c_count_response(i2c_msgs, num);
197         if (response_len < 0) {
198                 /* Unexpected; no errors should come when NULL response */
199                 dev_warn(dev, "Error preparing response %d\n", response_len);
200                 result = response_len;
201                 goto exit;
202         }
203
204         if (request_len <= ARRAY_SIZE(bus->request_buf)) {
205                 request = bus->request_buf;
206         } else {
207                 request = kzalloc(request_len, GFP_KERNEL);
208                 if (request == NULL) {
209                         result = -ENOMEM;
210                         goto exit;
211                 }
212         }
213         if (response_len <= ARRAY_SIZE(bus->response_buf)) {
214                 response = bus->response_buf;
215         } else {
216                 response = kzalloc(response_len, GFP_KERNEL);
217                 if (response == NULL) {
218                         result = -ENOMEM;
219                         goto exit;
220                 }
221         }
222
223         ec_i2c_construct_message(request, i2c_msgs, num, bus_num);
224
225         msg.version = 0;
226         msg.command = EC_CMD_I2C_PASSTHRU;
227         msg.outdata = request;
228         msg.outsize = request_len;
229         msg.indata = response;
230         msg.insize = response_len;
231
232         result = cros_ec_cmd_xfer(bus->ec, &msg);
233         if (result < 0)
234                 goto exit;
235
236         result = ec_i2c_parse_response(response, i2c_msgs, &num);
237         if (result < 0)
238                 goto exit;
239
240         /* Indicate success by saying how many messages were sent */
241         result = num;
242 exit:
243         if (request != bus->request_buf)
244                 kfree(request);
245         if (response != bus->response_buf)
246                 kfree(response);
247
248         return result;
249 }
250
251 static u32 ec_i2c_functionality(struct i2c_adapter *adap)
252 {
253         return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
254 }
255
256 static const struct i2c_algorithm ec_i2c_algorithm = {
257         .master_xfer    = ec_i2c_xfer,
258         .functionality  = ec_i2c_functionality,
259 };
260
261 static int ec_i2c_probe(struct platform_device *pdev)
262 {
263         struct device_node *np = pdev->dev.of_node;
264         struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
265         struct device *dev = &pdev->dev;
266         struct ec_i2c_device *bus = NULL;
267         u32 remote_bus;
268         int err;
269
270         if (!ec->cmd_xfer) {
271                 dev_err(dev, "Missing sendrecv\n");
272                 return -EINVAL;
273         }
274
275         bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
276         if (bus == NULL)
277                 return -ENOMEM;
278
279         err = of_property_read_u32(np, "google,remote-bus", &remote_bus);
280         if (err) {
281                 dev_err(dev, "Couldn't read remote-bus property\n");
282                 return err;
283         }
284         bus->remote_bus = remote_bus;
285
286         bus->ec = ec;
287         bus->dev = dev;
288
289         bus->adap.owner = THIS_MODULE;
290         strlcpy(bus->adap.name, "cros-ec-i2c-tunnel", sizeof(bus->adap.name));
291         bus->adap.algo = &ec_i2c_algorithm;
292         bus->adap.algo_data = bus;
293         bus->adap.dev.parent = &pdev->dev;
294         bus->adap.dev.of_node = np;
295         bus->adap.retries = I2C_MAX_RETRIES;
296
297         err = i2c_add_adapter(&bus->adap);
298         if (err) {
299                 dev_err(dev, "cannot register i2c adapter\n");
300                 return err;
301         }
302         platform_set_drvdata(pdev, bus);
303
304         return err;
305 }
306
307 static int ec_i2c_remove(struct platform_device *dev)
308 {
309         struct ec_i2c_device *bus = platform_get_drvdata(dev);
310
311         i2c_del_adapter(&bus->adap);
312
313         return 0;
314 }
315
316 static struct platform_driver ec_i2c_tunnel_driver = {
317         .probe = ec_i2c_probe,
318         .remove = ec_i2c_remove,
319         .driver = {
320                 .name = "cros-ec-i2c-tunnel",
321         },
322 };
323
324 module_platform_driver(ec_i2c_tunnel_driver);
325
326 MODULE_LICENSE("GPL");
327 MODULE_DESCRIPTION("EC I2C tunnel driver");
328 MODULE_ALIAS("platform:cros-ec-i2c-tunnel");