18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci// Expose an I2C passthrough to the ChromeOS EC.
38c2ecf20Sopenharmony_ci//
48c2ecf20Sopenharmony_ci// Copyright (C) 2013 Google, Inc.
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/acpi.h>
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/i2c.h>
98c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_commands.h>
108c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_proto.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#define I2C_MAX_RETRIES 3
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/**
178c2ecf20Sopenharmony_ci * struct ec_i2c_device - Driver data for I2C tunnel
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * @dev: Device node
208c2ecf20Sopenharmony_ci * @adap: I2C adapter
218c2ecf20Sopenharmony_ci * @ec: Pointer to EC device
228c2ecf20Sopenharmony_ci * @remote_bus: The EC bus number we tunnel to on the other side.
238c2ecf20Sopenharmony_ci * @request_buf: Buffer for transmitting data; we expect most transfers to fit.
248c2ecf20Sopenharmony_ci * @response_buf: Buffer for receiving data; we expect most transfers to fit.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct ec_i2c_device {
288c2ecf20Sopenharmony_ci	struct device *dev;
298c2ecf20Sopenharmony_ci	struct i2c_adapter adap;
308c2ecf20Sopenharmony_ci	struct cros_ec_device *ec;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	u16 remote_bus;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	u8 request_buf[256];
358c2ecf20Sopenharmony_ci	u8 response_buf[256];
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/**
398c2ecf20Sopenharmony_ci * ec_i2c_count_message - Count bytes needed for ec_i2c_construct_message
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci * @i2c_msgs: The i2c messages to read
428c2ecf20Sopenharmony_ci * @num: The number of i2c messages.
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * Returns the number of bytes the messages will take up.
458c2ecf20Sopenharmony_ci */
468c2ecf20Sopenharmony_cistatic int ec_i2c_count_message(const struct i2c_msg i2c_msgs[], int num)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	int i;
498c2ecf20Sopenharmony_ci	int size;
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci	size = sizeof(struct ec_params_i2c_passthru);
528c2ecf20Sopenharmony_ci	size += num * sizeof(struct ec_params_i2c_passthru_msg);
538c2ecf20Sopenharmony_ci	for (i = 0; i < num; i++)
548c2ecf20Sopenharmony_ci		if (!(i2c_msgs[i].flags & I2C_M_RD))
558c2ecf20Sopenharmony_ci			size += i2c_msgs[i].len;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return size;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/**
618c2ecf20Sopenharmony_ci * ec_i2c_construct_message - construct a message to go to the EC
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci * This function effectively stuffs the standard i2c_msg format of Linux into
648c2ecf20Sopenharmony_ci * a format that the EC understands.
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * @buf: The buffer to fill.  We assume that the buffer is big enough.
678c2ecf20Sopenharmony_ci * @i2c_msgs: The i2c messages to read.
688c2ecf20Sopenharmony_ci * @num: The number of i2c messages.
698c2ecf20Sopenharmony_ci * @bus_num: The remote bus number we want to talk to.
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * Returns 0 or a negative error number.
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_cistatic int ec_i2c_construct_message(u8 *buf, const struct i2c_msg i2c_msgs[],
748c2ecf20Sopenharmony_ci				    int num, u16 bus_num)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	struct ec_params_i2c_passthru *params;
778c2ecf20Sopenharmony_ci	u8 *out_data;
788c2ecf20Sopenharmony_ci	int i;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	out_data = buf + sizeof(struct ec_params_i2c_passthru) +
818c2ecf20Sopenharmony_ci		   num * sizeof(struct ec_params_i2c_passthru_msg);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	params = (struct ec_params_i2c_passthru *)buf;
848c2ecf20Sopenharmony_ci	params->port = bus_num;
858c2ecf20Sopenharmony_ci	params->num_msgs = num;
868c2ecf20Sopenharmony_ci	for (i = 0; i < num; i++) {
878c2ecf20Sopenharmony_ci		const struct i2c_msg *i2c_msg = &i2c_msgs[i];
888c2ecf20Sopenharmony_ci		struct ec_params_i2c_passthru_msg *msg = &params->msg[i];
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci		msg->len = i2c_msg->len;
918c2ecf20Sopenharmony_ci		msg->addr_flags = i2c_msg->addr;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci		if (i2c_msg->flags & I2C_M_TEN)
948c2ecf20Sopenharmony_ci			return -EINVAL;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci		if (i2c_msg->flags & I2C_M_RD) {
978c2ecf20Sopenharmony_ci			msg->addr_flags |= EC_I2C_FLAG_READ;
988c2ecf20Sopenharmony_ci		} else {
998c2ecf20Sopenharmony_ci			memcpy(out_data, i2c_msg->buf, msg->len);
1008c2ecf20Sopenharmony_ci			out_data += msg->len;
1018c2ecf20Sopenharmony_ci		}
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return 0;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/**
1088c2ecf20Sopenharmony_ci * ec_i2c_count_response - Count bytes needed for ec_i2c_parse_response
1098c2ecf20Sopenharmony_ci *
1108c2ecf20Sopenharmony_ci * @i2c_msgs: The i2c messages to to fill up.
1118c2ecf20Sopenharmony_ci * @num: The number of i2c messages expected.
1128c2ecf20Sopenharmony_ci *
1138c2ecf20Sopenharmony_ci * Returns the number of response bytes expeced.
1148c2ecf20Sopenharmony_ci */
1158c2ecf20Sopenharmony_cistatic int ec_i2c_count_response(struct i2c_msg i2c_msgs[], int num)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	int size;
1188c2ecf20Sopenharmony_ci	int i;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	size = sizeof(struct ec_response_i2c_passthru);
1218c2ecf20Sopenharmony_ci	for (i = 0; i < num; i++)
1228c2ecf20Sopenharmony_ci		if (i2c_msgs[i].flags & I2C_M_RD)
1238c2ecf20Sopenharmony_ci			size += i2c_msgs[i].len;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	return size;
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/**
1298c2ecf20Sopenharmony_ci * ec_i2c_parse_response - Parse a response from the EC
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * We'll take the EC's response and copy it back into msgs.
1328c2ecf20Sopenharmony_ci *
1338c2ecf20Sopenharmony_ci * @buf: The buffer to parse.
1348c2ecf20Sopenharmony_ci * @i2c_msgs: The i2c messages to to fill up.
1358c2ecf20Sopenharmony_ci * @num: The number of i2c messages; will be modified to include the actual
1368c2ecf20Sopenharmony_ci *	 number received.
1378c2ecf20Sopenharmony_ci *
1388c2ecf20Sopenharmony_ci * Returns 0 or a negative error number.
1398c2ecf20Sopenharmony_ci */
1408c2ecf20Sopenharmony_cistatic int ec_i2c_parse_response(const u8 *buf, struct i2c_msg i2c_msgs[],
1418c2ecf20Sopenharmony_ci				 int *num)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	const struct ec_response_i2c_passthru *resp;
1448c2ecf20Sopenharmony_ci	const u8 *in_data;
1458c2ecf20Sopenharmony_ci	int i;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	in_data = buf + sizeof(struct ec_response_i2c_passthru);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	resp = (const struct ec_response_i2c_passthru *)buf;
1508c2ecf20Sopenharmony_ci	if (resp->i2c_status & EC_I2C_STATUS_TIMEOUT)
1518c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
1528c2ecf20Sopenharmony_ci	else if (resp->i2c_status & EC_I2C_STATUS_NAK)
1538c2ecf20Sopenharmony_ci		return -ENXIO;
1548c2ecf20Sopenharmony_ci	else if (resp->i2c_status & EC_I2C_STATUS_ERROR)
1558c2ecf20Sopenharmony_ci		return -EIO;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/* Other side could send us back fewer messages, but not more */
1588c2ecf20Sopenharmony_ci	if (resp->num_msgs > *num)
1598c2ecf20Sopenharmony_ci		return -EPROTO;
1608c2ecf20Sopenharmony_ci	*num = resp->num_msgs;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	for (i = 0; i < *num; i++) {
1638c2ecf20Sopenharmony_ci		struct i2c_msg *i2c_msg = &i2c_msgs[i];
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci		if (i2c_msgs[i].flags & I2C_M_RD) {
1668c2ecf20Sopenharmony_ci			memcpy(i2c_msg->buf, in_data, i2c_msg->len);
1678c2ecf20Sopenharmony_ci			in_data += i2c_msg->len;
1688c2ecf20Sopenharmony_ci		}
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	return 0;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
1758c2ecf20Sopenharmony_ci		       int num)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	struct ec_i2c_device *bus = adap->algo_data;
1788c2ecf20Sopenharmony_ci	struct device *dev = bus->dev;
1798c2ecf20Sopenharmony_ci	const u16 bus_num = bus->remote_bus;
1808c2ecf20Sopenharmony_ci	int request_len;
1818c2ecf20Sopenharmony_ci	int response_len;
1828c2ecf20Sopenharmony_ci	int alloc_size;
1838c2ecf20Sopenharmony_ci	int result;
1848c2ecf20Sopenharmony_ci	struct cros_ec_command *msg;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	request_len = ec_i2c_count_message(i2c_msgs, num);
1878c2ecf20Sopenharmony_ci	if (request_len < 0) {
1888c2ecf20Sopenharmony_ci		dev_warn(dev, "Error constructing message %d\n", request_len);
1898c2ecf20Sopenharmony_ci		return request_len;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	response_len = ec_i2c_count_response(i2c_msgs, num);
1938c2ecf20Sopenharmony_ci	if (response_len < 0) {
1948c2ecf20Sopenharmony_ci		/* Unexpected; no errors should come when NULL response */
1958c2ecf20Sopenharmony_ci		dev_warn(dev, "Error preparing response %d\n", response_len);
1968c2ecf20Sopenharmony_ci		return response_len;
1978c2ecf20Sopenharmony_ci	}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	alloc_size = max(request_len, response_len);
2008c2ecf20Sopenharmony_ci	msg = kmalloc(sizeof(*msg) + alloc_size, GFP_KERNEL);
2018c2ecf20Sopenharmony_ci	if (!msg)
2028c2ecf20Sopenharmony_ci		return -ENOMEM;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	result = ec_i2c_construct_message(msg->data, i2c_msgs, num, bus_num);
2058c2ecf20Sopenharmony_ci	if (result) {
2068c2ecf20Sopenharmony_ci		dev_err(dev, "Error constructing EC i2c message %d\n", result);
2078c2ecf20Sopenharmony_ci		goto exit;
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	msg->version = 0;
2118c2ecf20Sopenharmony_ci	msg->command = EC_CMD_I2C_PASSTHRU;
2128c2ecf20Sopenharmony_ci	msg->outsize = request_len;
2138c2ecf20Sopenharmony_ci	msg->insize = response_len;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	result = cros_ec_cmd_xfer_status(bus->ec, msg);
2168c2ecf20Sopenharmony_ci	if (result < 0) {
2178c2ecf20Sopenharmony_ci		dev_err(dev, "Error transferring EC i2c message %d\n", result);
2188c2ecf20Sopenharmony_ci		goto exit;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	result = ec_i2c_parse_response(msg->data, i2c_msgs, &num);
2228c2ecf20Sopenharmony_ci	if (result < 0)
2238c2ecf20Sopenharmony_ci		goto exit;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/* Indicate success by saying how many messages were sent */
2268c2ecf20Sopenharmony_ci	result = num;
2278c2ecf20Sopenharmony_ciexit:
2288c2ecf20Sopenharmony_ci	kfree(msg);
2298c2ecf20Sopenharmony_ci	return result;
2308c2ecf20Sopenharmony_ci}
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cistatic u32 ec_i2c_functionality(struct i2c_adapter *adap)
2338c2ecf20Sopenharmony_ci{
2348c2ecf20Sopenharmony_ci	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic const struct i2c_algorithm ec_i2c_algorithm = {
2388c2ecf20Sopenharmony_ci	.master_xfer	= ec_i2c_xfer,
2398c2ecf20Sopenharmony_ci	.functionality	= ec_i2c_functionality,
2408c2ecf20Sopenharmony_ci};
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic int ec_i2c_probe(struct platform_device *pdev)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
2458c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2468c2ecf20Sopenharmony_ci	struct ec_i2c_device *bus = NULL;
2478c2ecf20Sopenharmony_ci	u32 remote_bus;
2488c2ecf20Sopenharmony_ci	int err;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	if (!ec->cmd_xfer) {
2518c2ecf20Sopenharmony_ci		dev_err(dev, "Missing sendrecv\n");
2528c2ecf20Sopenharmony_ci		return -EINVAL;
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
2568c2ecf20Sopenharmony_ci	if (bus == NULL)
2578c2ecf20Sopenharmony_ci		return -ENOMEM;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	err = device_property_read_u32(dev, "google,remote-bus", &remote_bus);
2608c2ecf20Sopenharmony_ci	if (err) {
2618c2ecf20Sopenharmony_ci		dev_err(dev, "Couldn't read remote-bus property\n");
2628c2ecf20Sopenharmony_ci		return err;
2638c2ecf20Sopenharmony_ci	}
2648c2ecf20Sopenharmony_ci	bus->remote_bus = remote_bus;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	bus->ec = ec;
2678c2ecf20Sopenharmony_ci	bus->dev = dev;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	bus->adap.owner = THIS_MODULE;
2708c2ecf20Sopenharmony_ci	strlcpy(bus->adap.name, "cros-ec-i2c-tunnel", sizeof(bus->adap.name));
2718c2ecf20Sopenharmony_ci	bus->adap.algo = &ec_i2c_algorithm;
2728c2ecf20Sopenharmony_ci	bus->adap.algo_data = bus;
2738c2ecf20Sopenharmony_ci	bus->adap.dev.parent = &pdev->dev;
2748c2ecf20Sopenharmony_ci	bus->adap.dev.of_node = pdev->dev.of_node;
2758c2ecf20Sopenharmony_ci	bus->adap.retries = I2C_MAX_RETRIES;
2768c2ecf20Sopenharmony_ci	ACPI_COMPANION_SET(&bus->adap.dev, ACPI_COMPANION(&pdev->dev));
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	err = i2c_add_adapter(&bus->adap);
2798c2ecf20Sopenharmony_ci	if (err)
2808c2ecf20Sopenharmony_ci		return err;
2818c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, bus);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	return err;
2848c2ecf20Sopenharmony_ci}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_cistatic int ec_i2c_remove(struct platform_device *dev)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct ec_i2c_device *bus = platform_get_drvdata(dev);
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	i2c_del_adapter(&bus->adap);
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	return 0;
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic const struct of_device_id cros_ec_i2c_of_match[] = {
2968c2ecf20Sopenharmony_ci	{ .compatible = "google,cros-ec-i2c-tunnel" },
2978c2ecf20Sopenharmony_ci	{},
2988c2ecf20Sopenharmony_ci};
2998c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, cros_ec_i2c_of_match);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic const struct acpi_device_id cros_ec_i2c_tunnel_acpi_id[] = {
3028c2ecf20Sopenharmony_ci	{ "GOOG0012", 0 },
3038c2ecf20Sopenharmony_ci	{ }
3048c2ecf20Sopenharmony_ci};
3058c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, cros_ec_i2c_tunnel_acpi_id);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic struct platform_driver ec_i2c_tunnel_driver = {
3088c2ecf20Sopenharmony_ci	.probe = ec_i2c_probe,
3098c2ecf20Sopenharmony_ci	.remove = ec_i2c_remove,
3108c2ecf20Sopenharmony_ci	.driver = {
3118c2ecf20Sopenharmony_ci		.name = "cros-ec-i2c-tunnel",
3128c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(cros_ec_i2c_tunnel_acpi_id),
3138c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(cros_ec_i2c_of_match),
3148c2ecf20Sopenharmony_ci	},
3158c2ecf20Sopenharmony_ci};
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_cimodule_platform_driver(ec_i2c_tunnel_driver);
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
3208c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("EC I2C tunnel driver");
3218c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:cros-ec-i2c-tunnel");
322