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