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>
19 #define I2C_MAX_RETRIES 3
22 * struct ec_i2c_device - Driver data for I2C tunnel
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.
32 struct ec_i2c_device
{
34 struct i2c_adapter adap
;
35 struct cros_ec_device
*ec
;
44 * ec_i2c_count_message - Count bytes needed for ec_i2c_construct_message
46 * @i2c_msgs: The i2c messages to read
47 * @num: The number of i2c messages.
49 * Returns the number of bytes the messages will take up.
51 static int ec_i2c_count_message(const struct i2c_msg i2c_msgs
[], int num
)
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
;
66 * ec_i2c_construct_message - construct a message to go to the EC
68 * This function effectively stuffs the standard i2c_msg format of Linux into
69 * a format that the EC understands.
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.
76 * Returns 0 or a negative error number.
78 static int ec_i2c_construct_message(u8
*buf
, const struct i2c_msg i2c_msgs
[],
81 struct ec_params_i2c_passthru
*params
;
85 out_data
= buf
+ sizeof(struct ec_params_i2c_passthru
) +
86 num
* sizeof(struct ec_params_i2c_passthru_msg
);
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
];
95 msg
->len
= i2c_msg
->len
;
96 msg
->addr_flags
= i2c_msg
->addr
;
98 if (i2c_msg
->flags
& I2C_M_TEN
)
101 if (i2c_msg
->flags
& I2C_M_RD
) {
102 msg
->addr_flags
|= EC_I2C_FLAG_READ
;
104 memcpy(out_data
, i2c_msg
->buf
, msg
->len
);
105 out_data
+= msg
->len
;
113 * ec_i2c_count_response - Count bytes needed for ec_i2c_parse_response
115 * @i2c_msgs: The i2c messages to to fill up.
116 * @num: The number of i2c messages expected.
118 * Returns the number of response bytes expeced.
120 static int ec_i2c_count_response(struct i2c_msg i2c_msgs
[], int num
)
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
;
134 * ec_i2c_parse_response - Parse a response from the EC
136 * We'll take the EC's response and copy it back into msgs.
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
143 * Returns 0 or a negative error number.
145 static int ec_i2c_parse_response(const u8
*buf
, struct i2c_msg i2c_msgs
[],
148 const struct ec_response_i2c_passthru
*resp
;
152 in_data
= buf
+ sizeof(struct ec_response_i2c_passthru
);
154 resp
= (const struct ec_response_i2c_passthru
*)buf
;
155 if (resp
->i2c_status
& EC_I2C_STATUS_TIMEOUT
)
157 else if (resp
->i2c_status
& EC_I2C_STATUS_NAK
)
159 else if (resp
->i2c_status
& EC_I2C_STATUS_ERROR
)
162 /* Other side could send us back fewer messages, but not more */
163 if (resp
->num_msgs
> *num
)
165 *num
= resp
->num_msgs
;
167 for (i
= 0; i
< *num
; i
++) {
168 struct i2c_msg
*i2c_msg
= &i2c_msgs
[i
];
170 if (i2c_msgs
[i
].flags
& I2C_M_RD
) {
171 memcpy(i2c_msg
->buf
, in_data
, i2c_msg
->len
);
172 in_data
+= i2c_msg
->len
;
179 static int ec_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg i2c_msgs
[],
182 struct ec_i2c_device
*bus
= adap
->algo_data
;
183 struct device
*dev
= bus
->dev
;
184 const u16 bus_num
= bus
->remote_bus
;
189 struct cros_ec_command
*msg
;
191 request_len
= ec_i2c_count_message(i2c_msgs
, num
);
192 if (request_len
< 0) {
193 dev_warn(dev
, "Error constructing message %d\n", request_len
);
197 response_len
= ec_i2c_count_response(i2c_msgs
, num
);
198 if (response_len
< 0) {
199 /* Unexpected; no errors should come when NULL response */
200 dev_warn(dev
, "Error preparing response %d\n", response_len
);
204 alloc_size
= max(request_len
, response_len
);
205 msg
= kmalloc(sizeof(*msg
) + alloc_size
, GFP_KERNEL
);
209 result
= ec_i2c_construct_message(msg
->data
, i2c_msgs
, num
, bus_num
);
211 dev_err(dev
, "Error constructing EC i2c message %d\n", result
);
216 msg
->command
= EC_CMD_I2C_PASSTHRU
;
217 msg
->outsize
= request_len
;
218 msg
->insize
= response_len
;
220 result
= cros_ec_cmd_xfer_status(bus
->ec
, msg
);
222 dev_err(dev
, "Error transferring EC i2c message %d\n", result
);
226 result
= ec_i2c_parse_response(msg
->data
, i2c_msgs
, &num
);
230 /* Indicate success by saying how many messages were sent */
237 static u32
ec_i2c_functionality(struct i2c_adapter
*adap
)
239 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
242 static const struct i2c_algorithm ec_i2c_algorithm
= {
243 .master_xfer
= ec_i2c_xfer
,
244 .functionality
= ec_i2c_functionality
,
247 static int ec_i2c_probe(struct platform_device
*pdev
)
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
;
257 dev_err(dev
, "Missing sendrecv\n");
261 bus
= devm_kzalloc(dev
, sizeof(*bus
), GFP_KERNEL
);
265 err
= of_property_read_u32(np
, "google,remote-bus", &remote_bus
);
267 dev_err(dev
, "Couldn't read remote-bus property\n");
270 bus
->remote_bus
= remote_bus
;
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
;
283 err
= i2c_add_adapter(&bus
->adap
);
286 platform_set_drvdata(pdev
, bus
);
291 static int ec_i2c_remove(struct platform_device
*dev
)
293 struct ec_i2c_device
*bus
= platform_get_drvdata(dev
);
295 i2c_del_adapter(&bus
->adap
);
301 static const struct of_device_id cros_ec_i2c_of_match
[] = {
302 { .compatible
= "google,cros-ec-i2c-tunnel" },
305 MODULE_DEVICE_TABLE(of
, cros_ec_i2c_of_match
);
308 static struct platform_driver ec_i2c_tunnel_driver
= {
309 .probe
= ec_i2c_probe
,
310 .remove
= ec_i2c_remove
,
312 .name
= "cros-ec-i2c-tunnel",
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");