1 // SPDX-License-Identifier: GPL-2.0+
2 // Expose an I2C passthrough to the ChromeOS EC.
4 // Copyright (C) 2013 Google, Inc.
6 #include <linux/acpi.h>
7 #include <linux/module.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
17 * struct ec_i2c_device - Driver data for I2C tunnel
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
{
29 struct i2c_adapter adap
;
30 struct cros_ec_device
*ec
;
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
)
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
;
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
[],
76 struct ec_params_i2c_passthru
*params
;
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
= ¶ms
->msg
[i
];
90 msg
->len
= i2c_msg
->len
;
91 msg
->addr_flags
= i2c_msg
->addr
;
93 if (i2c_msg
->flags
& I2C_M_TEN
)
96 if (i2c_msg
->flags
& I2C_M_RD
) {
97 msg
->addr_flags
|= EC_I2C_FLAG_READ
;
99 memcpy(out_data
, i2c_msg
->buf
, msg
->len
);
100 out_data
+= msg
->len
;
108 * ec_i2c_count_response - Count bytes needed for ec_i2c_parse_response
110 * @i2c_msgs: The i2c messages 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
)
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
;
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 fill up.
135 * @num: The number of i2c messages; will be modified to include the actual
138 * Returns 0 or a negative error number.
140 static int ec_i2c_parse_response(const u8
*buf
, struct i2c_msg i2c_msgs
[],
143 const struct ec_response_i2c_passthru
*resp
;
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
)
152 else if (resp
->i2c_status
& EC_I2C_STATUS_NAK
)
154 else if (resp
->i2c_status
& EC_I2C_STATUS_ERROR
)
157 /* Other side could send us back fewer messages, but not more */
158 if (resp
->num_msgs
> *num
)
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
;
174 static int ec_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg i2c_msgs
[],
177 struct ec_i2c_device
*bus
= adap
->algo_data
;
178 struct device
*dev
= bus
->dev
;
179 const u16 bus_num
= bus
->remote_bus
;
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
);
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
);
199 alloc_size
= max(request_len
, response_len
);
200 msg
= kmalloc(sizeof(*msg
) + alloc_size
, GFP_KERNEL
);
204 result
= ec_i2c_construct_message(msg
->data
, i2c_msgs
, num
, bus_num
);
206 dev_err(dev
, "Error constructing EC i2c message %d\n", result
);
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
);
217 dev_err(dev
, "Error transferring EC i2c message %d\n", result
);
221 result
= ec_i2c_parse_response(msg
->data
, i2c_msgs
, &num
);
225 /* Indicate success by saying how many messages were sent */
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
= {
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
;
251 dev_err(dev
, "Missing sendrecv\n");
255 bus
= devm_kzalloc(dev
, sizeof(*bus
), GFP_KERNEL
);
259 err
= device_property_read_u32(dev
, "google,remote-bus", &remote_bus
);
261 dev_err(dev
, "Couldn't read remote-bus property\n");
264 bus
->remote_bus
= remote_bus
;
269 bus
->adap
.owner
= THIS_MODULE
;
270 strscpy(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
);
281 platform_set_drvdata(pdev
, bus
);
286 static void ec_i2c_remove(struct platform_device
*dev
)
288 struct ec_i2c_device
*bus
= platform_get_drvdata(dev
);
290 i2c_del_adapter(&bus
->adap
);
293 static const struct of_device_id cros_ec_i2c_of_match
[] __maybe_unused
= {
294 { .compatible
= "google,cros-ec-i2c-tunnel" },
297 MODULE_DEVICE_TABLE(of
, cros_ec_i2c_of_match
);
299 static const struct acpi_device_id cros_ec_i2c_tunnel_acpi_id
[] __maybe_unused
= {
303 MODULE_DEVICE_TABLE(acpi
, cros_ec_i2c_tunnel_acpi_id
);
305 static struct platform_driver ec_i2c_tunnel_driver
= {
306 .probe
= ec_i2c_probe
,
307 .remove
= ec_i2c_remove
,
309 .name
= "cros-ec-i2c-tunnel",
310 .acpi_match_table
= ACPI_PTR(cros_ec_i2c_tunnel_acpi_id
),
311 .of_match_table
= of_match_ptr(cros_ec_i2c_of_match
),
315 module_platform_driver(ec_i2c_tunnel_driver
);
317 MODULE_LICENSE("GPL");
318 MODULE_DESCRIPTION("EC I2C tunnel driver");
319 MODULE_ALIAS("platform:cros-ec-i2c-tunnel");