1
2
3
4
5
6
7
8
9
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
23
24
25
26
27
28
29
30
31
32struct 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
45
46
47
48
49
50
51static 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
67
68
69
70
71
72
73
74
75
76
77
78static 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 = ¶ms->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 return -EINVAL;
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
114
115
116
117
118
119
120static 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
135
136
137
138
139
140
141
142
143
144
145static 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
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
177static 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 int alloc_size;
186 int result;
187 struct cros_ec_command *msg;
188
189 request_len = ec_i2c_count_message(i2c_msgs, num);
190 if (request_len < 0) {
191 dev_warn(dev, "Error constructing message %d\n", request_len);
192 return request_len;
193 }
194
195 response_len = ec_i2c_count_response(i2c_msgs, num);
196 if (response_len < 0) {
197
198 dev_warn(dev, "Error preparing response %d\n", response_len);
199 return response_len;
200 }
201
202 alloc_size = max(request_len, response_len);
203 msg = kmalloc(sizeof(*msg) + alloc_size, GFP_KERNEL);
204 if (!msg)
205 return -ENOMEM;
206
207 result = ec_i2c_construct_message(msg->data, i2c_msgs, num, bus_num);
208 if (result) {
209 dev_err(dev, "Error constructing EC i2c message %d\n", result);
210 goto exit;
211 }
212
213 msg->version = 0;
214 msg->command = EC_CMD_I2C_PASSTHRU;
215 msg->outsize = request_len;
216 msg->insize = response_len;
217
218 result = cros_ec_cmd_xfer_status(bus->ec, msg);
219 if (result < 0) {
220 dev_err(dev, "Error transferring EC i2c message %d\n", result);
221 goto exit;
222 }
223
224 result = ec_i2c_parse_response(msg->data, i2c_msgs, &num);
225 if (result < 0) {
226 dev_err(dev, "Error parsing EC i2c message %d\n", result);
227 goto exit;
228 }
229
230
231 result = num;
232exit:
233 kfree(msg);
234 return result;
235}
236
237static u32 ec_i2c_functionality(struct i2c_adapter *adap)
238{
239 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
240}
241
242static const struct i2c_algorithm ec_i2c_algorithm = {
243 .master_xfer = ec_i2c_xfer,
244 .functionality = ec_i2c_functionality,
245};
246
247static int ec_i2c_probe(struct platform_device *pdev)
248{
249 struct device_node *np = pdev->dev.of_node;
250 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
251 struct device *dev = &pdev->dev;
252 struct ec_i2c_device *bus = NULL;
253 u32 remote_bus;
254 int err;
255
256 if (!ec->cmd_xfer) {
257 dev_err(dev, "Missing sendrecv\n");
258 return -EINVAL;
259 }
260
261 bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
262 if (bus == NULL)
263 return -ENOMEM;
264
265 err = of_property_read_u32(np, "google,remote-bus", &remote_bus);
266 if (err) {
267 dev_err(dev, "Couldn't read remote-bus property\n");
268 return err;
269 }
270 bus->remote_bus = remote_bus;
271
272 bus->ec = ec;
273 bus->dev = dev;
274
275 bus->adap.owner = THIS_MODULE;
276 strlcpy(bus->adap.name, "cros-ec-i2c-tunnel", sizeof(bus->adap.name));
277 bus->adap.algo = &ec_i2c_algorithm;
278 bus->adap.algo_data = bus;
279 bus->adap.dev.parent = &pdev->dev;
280 bus->adap.dev.of_node = np;
281 bus->adap.retries = I2C_MAX_RETRIES;
282
283 err = i2c_add_adapter(&bus->adap);
284 if (err)
285 return err;
286 platform_set_drvdata(pdev, bus);
287
288 return err;
289}
290
291static int ec_i2c_remove(struct platform_device *dev)
292{
293 struct ec_i2c_device *bus = platform_get_drvdata(dev);
294
295 i2c_del_adapter(&bus->adap);
296
297 return 0;
298}
299
300#ifdef CONFIG_OF
301static const struct of_device_id cros_ec_i2c_of_match[] = {
302 { .compatible = "google,cros-ec-i2c-tunnel" },
303 {},
304};
305MODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
306#endif
307
308static struct platform_driver ec_i2c_tunnel_driver = {
309 .probe = ec_i2c_probe,
310 .remove = ec_i2c_remove,
311 .driver = {
312 .name = "cros-ec-i2c-tunnel",
313 .of_match_table = of_match_ptr(cros_ec_i2c_of_match),
314 },
315};
316
317module_platform_driver(ec_i2c_tunnel_driver);
318
319MODULE_LICENSE("GPL");
320MODULE_DESCRIPTION("EC I2C tunnel driver");
321MODULE_ALIAS("platform:cros-ec-i2c-tunnel");
322