xref: /linux/drivers/gpio/gpio-cros-ec.c (revision 782f4e47ffc19622bf80b3c0cf9cadd2b0b9a644)
1f837fe1bSStephen Boyd // SPDX-License-Identifier: GPL-2.0-only
2f837fe1bSStephen Boyd /*
3f837fe1bSStephen Boyd  * Copyright 2024 Google LLC
4f837fe1bSStephen Boyd  *
5f837fe1bSStephen Boyd  * This driver provides the ability to control GPIOs on the Chrome OS EC.
6f837fe1bSStephen Boyd  * There isn't any direction control, and setting values on GPIOs is only
7f837fe1bSStephen Boyd  * possible when the system is unlocked.
8f837fe1bSStephen Boyd  */
9f837fe1bSStephen Boyd 
10f837fe1bSStephen Boyd #include <linux/bitops.h>
11f837fe1bSStephen Boyd #include <linux/device.h>
12f837fe1bSStephen Boyd #include <linux/errno.h>
13f837fe1bSStephen Boyd #include <linux/gpio/driver.h>
14f837fe1bSStephen Boyd #include <linux/kernel.h>
15*782f4e47STzung-Bi Shih #include <linux/mod_devicetable.h>
16f837fe1bSStephen Boyd #include <linux/module.h>
17f837fe1bSStephen Boyd #include <linux/platform_data/cros_ec_commands.h>
18f837fe1bSStephen Boyd #include <linux/platform_data/cros_ec_proto.h>
19f837fe1bSStephen Boyd #include <linux/platform_device.h>
20f837fe1bSStephen Boyd #include <linux/property.h>
21f837fe1bSStephen Boyd #include <linux/slab.h>
22f837fe1bSStephen Boyd 
23f837fe1bSStephen Boyd /* Prefix all names to avoid collisions with EC <-> AP nets */
24f837fe1bSStephen Boyd static const char cros_ec_gpio_prefix[] = "EC:";
25f837fe1bSStephen Boyd 
26f837fe1bSStephen Boyd /* Setting gpios is only supported when the system is unlocked */
27f837fe1bSStephen Boyd static void cros_ec_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
28f837fe1bSStephen Boyd {
29f837fe1bSStephen Boyd 	const char *name = gc->names[gpio] + strlen(cros_ec_gpio_prefix);
30f837fe1bSStephen Boyd 	struct cros_ec_device *cros_ec = gpiochip_get_data(gc);
31f837fe1bSStephen Boyd 	struct ec_params_gpio_set params = {
32f837fe1bSStephen Boyd 		.val = val,
33f837fe1bSStephen Boyd 	};
34f837fe1bSStephen Boyd 	int ret;
35f837fe1bSStephen Boyd 	ssize_t copied;
36f837fe1bSStephen Boyd 
37f837fe1bSStephen Boyd 	copied = strscpy(params.name, name, sizeof(params.name));
38f837fe1bSStephen Boyd 	if (copied < 0)
39f837fe1bSStephen Boyd 		return;
40f837fe1bSStephen Boyd 
41f837fe1bSStephen Boyd 	ret = cros_ec_cmd(cros_ec, 0, EC_CMD_GPIO_SET, &params,
42f837fe1bSStephen Boyd 			  sizeof(params), NULL, 0);
43f837fe1bSStephen Boyd 	if (ret < 0)
44f837fe1bSStephen Boyd 		dev_err(gc->parent, "error setting gpio%d (%s) on EC: %d\n", gpio, name, ret);
45f837fe1bSStephen Boyd }
46f837fe1bSStephen Boyd 
47f837fe1bSStephen Boyd static int cros_ec_gpio_get(struct gpio_chip *gc, unsigned int gpio)
48f837fe1bSStephen Boyd {
49f837fe1bSStephen Boyd 	const char *name = gc->names[gpio] + strlen(cros_ec_gpio_prefix);
50f837fe1bSStephen Boyd 	struct cros_ec_device *cros_ec = gpiochip_get_data(gc);
51f837fe1bSStephen Boyd 	struct ec_params_gpio_get params;
52f837fe1bSStephen Boyd 	struct ec_response_gpio_get response;
53f837fe1bSStephen Boyd 	int ret;
54f837fe1bSStephen Boyd 	ssize_t copied;
55f837fe1bSStephen Boyd 
56f837fe1bSStephen Boyd 	copied = strscpy(params.name, name, sizeof(params.name));
57f837fe1bSStephen Boyd 	if (copied < 0)
58f837fe1bSStephen Boyd 		return -EINVAL;
59f837fe1bSStephen Boyd 
60f837fe1bSStephen Boyd 	ret = cros_ec_cmd(cros_ec, 0, EC_CMD_GPIO_GET, &params,
61f837fe1bSStephen Boyd 			  sizeof(params), &response, sizeof(response));
62f837fe1bSStephen Boyd 	if (ret < 0) {
63f837fe1bSStephen Boyd 		dev_err(gc->parent, "error getting gpio%d (%s) on EC: %d\n", gpio, name, ret);
64f837fe1bSStephen Boyd 		return ret;
65f837fe1bSStephen Boyd 	}
66f837fe1bSStephen Boyd 
67f837fe1bSStephen Boyd 	return response.val;
68f837fe1bSStephen Boyd }
69f837fe1bSStephen Boyd 
70f837fe1bSStephen Boyd #define CROS_EC_GPIO_INPUT         BIT(8)
71f837fe1bSStephen Boyd #define CROS_EC_GPIO_OUTPUT        BIT(9)
72f837fe1bSStephen Boyd 
73f837fe1bSStephen Boyd static int cros_ec_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
74f837fe1bSStephen Boyd {
75f837fe1bSStephen Boyd 	const char *name = gc->names[gpio] + strlen(cros_ec_gpio_prefix);
76f837fe1bSStephen Boyd 	struct cros_ec_device *cros_ec = gpiochip_get_data(gc);
77f837fe1bSStephen Boyd 	struct ec_params_gpio_get_v1 params = {
78f837fe1bSStephen Boyd 		.subcmd = EC_GPIO_GET_INFO,
79f837fe1bSStephen Boyd 		.get_info.index = gpio,
80f837fe1bSStephen Boyd 	};
81f837fe1bSStephen Boyd 	struct ec_response_gpio_get_v1 response;
82f837fe1bSStephen Boyd 	int ret;
83f837fe1bSStephen Boyd 
84f837fe1bSStephen Boyd 	ret = cros_ec_cmd(cros_ec, 1, EC_CMD_GPIO_GET, &params,
85f837fe1bSStephen Boyd 			  sizeof(params), &response, sizeof(response));
86f837fe1bSStephen Boyd 	if (ret < 0) {
87f837fe1bSStephen Boyd 		dev_err(gc->parent, "error getting direction of gpio%d (%s) on EC: %d\n", gpio, name, ret);
88f837fe1bSStephen Boyd 		return ret;
89f837fe1bSStephen Boyd 	}
90f837fe1bSStephen Boyd 
91f837fe1bSStephen Boyd 	if (response.get_info.flags & CROS_EC_GPIO_INPUT)
92f837fe1bSStephen Boyd 		return GPIO_LINE_DIRECTION_IN;
93f837fe1bSStephen Boyd 
94f837fe1bSStephen Boyd 	if (response.get_info.flags & CROS_EC_GPIO_OUTPUT)
95f837fe1bSStephen Boyd 		return GPIO_LINE_DIRECTION_OUT;
96f837fe1bSStephen Boyd 
97f837fe1bSStephen Boyd 	return -EINVAL;
98f837fe1bSStephen Boyd }
99f837fe1bSStephen Boyd 
100f837fe1bSStephen Boyd /* Query EC for all gpio line names */
101f837fe1bSStephen Boyd static int cros_ec_gpio_init_names(struct cros_ec_device *cros_ec, struct gpio_chip *gc)
102f837fe1bSStephen Boyd {
103f837fe1bSStephen Boyd 	struct ec_params_gpio_get_v1 params = {
104f837fe1bSStephen Boyd 		.subcmd = EC_GPIO_GET_INFO,
105f837fe1bSStephen Boyd 	};
106f837fe1bSStephen Boyd 	struct ec_response_gpio_get_v1 response;
107f837fe1bSStephen Boyd 	int ret, i;
108f837fe1bSStephen Boyd 	/* EC may not NUL terminate */
109f837fe1bSStephen Boyd 	size_t name_len = strlen(cros_ec_gpio_prefix) + sizeof(response.get_info.name) + 1;
110f837fe1bSStephen Boyd 	ssize_t copied;
111f837fe1bSStephen Boyd 	const char **names;
112f837fe1bSStephen Boyd 	char *str;
113f837fe1bSStephen Boyd 
114f837fe1bSStephen Boyd 	names = devm_kcalloc(gc->parent, gc->ngpio, sizeof(*names), GFP_KERNEL);
115f837fe1bSStephen Boyd 	if (!names)
116f837fe1bSStephen Boyd 		return -ENOMEM;
117f837fe1bSStephen Boyd 	gc->names = names;
118f837fe1bSStephen Boyd 
119f837fe1bSStephen Boyd 	str = devm_kcalloc(gc->parent, gc->ngpio, name_len, GFP_KERNEL);
120f837fe1bSStephen Boyd 	if (!str)
121f837fe1bSStephen Boyd 		return -ENOMEM;
122f837fe1bSStephen Boyd 
123f837fe1bSStephen Boyd 	/* Get gpio line names one at a time */
124f837fe1bSStephen Boyd 	for (i = 0; i < gc->ngpio; i++) {
125f837fe1bSStephen Boyd 		params.get_info.index = i;
126f837fe1bSStephen Boyd 		ret = cros_ec_cmd(cros_ec, 1, EC_CMD_GPIO_GET, &params,
127f837fe1bSStephen Boyd 				  sizeof(params), &response, sizeof(response));
128f837fe1bSStephen Boyd 		if (ret < 0) {
129f837fe1bSStephen Boyd 			dev_err_probe(gc->parent, ret, "error getting gpio%d info\n", i);
130f837fe1bSStephen Boyd 			return ret;
131f837fe1bSStephen Boyd 		}
132f837fe1bSStephen Boyd 
133f837fe1bSStephen Boyd 		names[i] = str;
134f837fe1bSStephen Boyd 		copied = scnprintf(str, name_len, "%s%s", cros_ec_gpio_prefix,
135f837fe1bSStephen Boyd 				   response.get_info.name);
136f837fe1bSStephen Boyd 		if (copied < 0)
137f837fe1bSStephen Boyd 			return copied;
138f837fe1bSStephen Boyd 
139f837fe1bSStephen Boyd 		str += copied + 1;
140f837fe1bSStephen Boyd 	}
141f837fe1bSStephen Boyd 
142f837fe1bSStephen Boyd 	return 0;
143f837fe1bSStephen Boyd }
144f837fe1bSStephen Boyd 
145f837fe1bSStephen Boyd /* Query EC for number of gpios */
146f837fe1bSStephen Boyd static int cros_ec_gpio_ngpios(struct cros_ec_device *cros_ec)
147f837fe1bSStephen Boyd {
148f837fe1bSStephen Boyd 	struct ec_params_gpio_get_v1 params = {
149f837fe1bSStephen Boyd 		.subcmd = EC_GPIO_GET_COUNT,
150f837fe1bSStephen Boyd 	};
151f837fe1bSStephen Boyd 	struct ec_response_gpio_get_v1 response;
152f837fe1bSStephen Boyd 	int ret;
153f837fe1bSStephen Boyd 
154f837fe1bSStephen Boyd 	ret = cros_ec_cmd(cros_ec, 1, EC_CMD_GPIO_GET, &params,
155f837fe1bSStephen Boyd 			  sizeof(params), &response, sizeof(response));
156f837fe1bSStephen Boyd 	if (ret < 0)
157f837fe1bSStephen Boyd 		return ret;
158f837fe1bSStephen Boyd 
159f837fe1bSStephen Boyd 	return response.get_count.val;
160f837fe1bSStephen Boyd }
161f837fe1bSStephen Boyd 
162f837fe1bSStephen Boyd static int cros_ec_gpio_probe(struct platform_device *pdev)
163f837fe1bSStephen Boyd {
164f837fe1bSStephen Boyd 	struct device *dev = &pdev->dev;
165f837fe1bSStephen Boyd 	struct device *parent = dev->parent;
166f837fe1bSStephen Boyd 	struct cros_ec_dev *ec_dev = dev_get_drvdata(parent);
167f837fe1bSStephen Boyd 	struct cros_ec_device *cros_ec = ec_dev->ec_dev;
168f837fe1bSStephen Boyd 	struct gpio_chip *gc;
169f837fe1bSStephen Boyd 	int ngpios;
170f837fe1bSStephen Boyd 	int ret;
171f837fe1bSStephen Boyd 
172f837fe1bSStephen Boyd 	/* Use the fwnode from the protocol device, e.g. cros-ec-spi */
173f837fe1bSStephen Boyd 	device_set_node(dev, dev_fwnode(cros_ec->dev));
174f837fe1bSStephen Boyd 
175f837fe1bSStephen Boyd 	ngpios = cros_ec_gpio_ngpios(cros_ec);
176f837fe1bSStephen Boyd 	if (ngpios < 0) {
177f837fe1bSStephen Boyd 		dev_err_probe(dev, ngpios, "error getting gpio count\n");
178f837fe1bSStephen Boyd 		return ngpios;
179f837fe1bSStephen Boyd 	}
180f837fe1bSStephen Boyd 
181f837fe1bSStephen Boyd 	gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
182f837fe1bSStephen Boyd 	if (!gc)
183f837fe1bSStephen Boyd 		return -ENOMEM;
184f837fe1bSStephen Boyd 
185f837fe1bSStephen Boyd 	gc->ngpio = ngpios;
186f837fe1bSStephen Boyd 	gc->parent = dev;
187f837fe1bSStephen Boyd 	ret = cros_ec_gpio_init_names(cros_ec, gc);
188f837fe1bSStephen Boyd 	if (ret)
189f837fe1bSStephen Boyd 		return ret;
190f837fe1bSStephen Boyd 
191f837fe1bSStephen Boyd 	gc->can_sleep = true;
192f837fe1bSStephen Boyd 	gc->label = dev_name(dev);
193f837fe1bSStephen Boyd 	gc->base = -1;
194f837fe1bSStephen Boyd 	gc->set = cros_ec_gpio_set;
195f837fe1bSStephen Boyd 	gc->get = cros_ec_gpio_get;
196f837fe1bSStephen Boyd 	gc->get_direction = cros_ec_gpio_get_direction;
197f837fe1bSStephen Boyd 
198f837fe1bSStephen Boyd 	return devm_gpiochip_add_data(dev, gc, cros_ec);
199f837fe1bSStephen Boyd }
200f837fe1bSStephen Boyd 
201*782f4e47STzung-Bi Shih static const struct platform_device_id cros_ec_gpio_id[] = {
202*782f4e47STzung-Bi Shih 	{ "cros-ec-gpio", 0 },
203*782f4e47STzung-Bi Shih 	{}
204*782f4e47STzung-Bi Shih };
205*782f4e47STzung-Bi Shih MODULE_DEVICE_TABLE(platform, cros_ec_gpio_id);
206*782f4e47STzung-Bi Shih 
207f837fe1bSStephen Boyd static struct platform_driver cros_ec_gpio_driver = {
208f837fe1bSStephen Boyd 	.probe = cros_ec_gpio_probe,
209f837fe1bSStephen Boyd 	.driver = {
210f837fe1bSStephen Boyd 		.name = "cros-ec-gpio",
211f837fe1bSStephen Boyd 	},
212*782f4e47STzung-Bi Shih 	.id_table = cros_ec_gpio_id,
213f837fe1bSStephen Boyd };
214f837fe1bSStephen Boyd module_platform_driver(cros_ec_gpio_driver);
215f837fe1bSStephen Boyd 
216f837fe1bSStephen Boyd MODULE_DESCRIPTION("ChromeOS EC GPIO Driver");
217f837fe1bSStephen Boyd MODULE_LICENSE("GPL");
218