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 int 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 	ssize_t copied;
35 
36 	copied = strscpy(params.name, name, sizeof(params.name));
37 	if (copied < 0)
38 		return copied;
39 
40 	return cros_ec_cmd(cros_ec, 0, EC_CMD_GPIO_SET, &params,
41 			   sizeof(params), NULL, 0);
42 }
43 
cros_ec_gpio_get(struct gpio_chip * gc,unsigned int gpio)44 static int cros_ec_gpio_get(struct gpio_chip *gc, unsigned int gpio)
45 {
46 	const char *name = gc->names[gpio] + strlen(cros_ec_gpio_prefix);
47 	struct cros_ec_device *cros_ec = gpiochip_get_data(gc);
48 	struct ec_params_gpio_get params;
49 	struct ec_response_gpio_get response;
50 	int ret;
51 	ssize_t copied;
52 
53 	copied = strscpy(params.name, name, sizeof(params.name));
54 	if (copied < 0)
55 		return -EINVAL;
56 
57 	ret = cros_ec_cmd(cros_ec, 0, EC_CMD_GPIO_GET, &params,
58 			  sizeof(params), &response, sizeof(response));
59 	if (ret < 0) {
60 		dev_err(gc->parent, "error getting gpio%d (%s) on EC: %d\n", gpio, name, ret);
61 		return ret;
62 	}
63 
64 	return response.val;
65 }
66 
67 #define CROS_EC_GPIO_INPUT         BIT(8)
68 #define CROS_EC_GPIO_OUTPUT        BIT(9)
69 
cros_ec_gpio_get_direction(struct gpio_chip * gc,unsigned int gpio)70 static int cros_ec_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
71 {
72 	const char *name = gc->names[gpio] + strlen(cros_ec_gpio_prefix);
73 	struct cros_ec_device *cros_ec = gpiochip_get_data(gc);
74 	struct ec_params_gpio_get_v1 params = {
75 		.subcmd = EC_GPIO_GET_INFO,
76 		.get_info.index = gpio,
77 	};
78 	struct ec_response_gpio_get_v1 response;
79 	int ret;
80 
81 	ret = cros_ec_cmd(cros_ec, 1, EC_CMD_GPIO_GET, &params,
82 			  sizeof(params), &response, sizeof(response));
83 	if (ret < 0) {
84 		dev_err(gc->parent, "error getting direction of gpio%d (%s) on EC: %d\n", gpio, name, ret);
85 		return ret;
86 	}
87 
88 	if (response.get_info.flags & CROS_EC_GPIO_INPUT)
89 		return GPIO_LINE_DIRECTION_IN;
90 
91 	if (response.get_info.flags & CROS_EC_GPIO_OUTPUT)
92 		return GPIO_LINE_DIRECTION_OUT;
93 
94 	return -EINVAL;
95 }
96 
97 /* Query EC for all gpio line names */
cros_ec_gpio_init_names(struct cros_ec_device * cros_ec,struct gpio_chip * gc)98 static int cros_ec_gpio_init_names(struct cros_ec_device *cros_ec, struct gpio_chip *gc)
99 {
100 	struct ec_params_gpio_get_v1 params = {
101 		.subcmd = EC_GPIO_GET_INFO,
102 	};
103 	struct ec_response_gpio_get_v1 response;
104 	int ret, i;
105 	/* EC may not NUL terminate */
106 	size_t name_len = strlen(cros_ec_gpio_prefix) + sizeof(response.get_info.name) + 1;
107 	ssize_t copied;
108 	const char **names;
109 	char *str;
110 
111 	names = devm_kcalloc(gc->parent, gc->ngpio, sizeof(*names), GFP_KERNEL);
112 	if (!names)
113 		return -ENOMEM;
114 	gc->names = names;
115 
116 	str = devm_kcalloc(gc->parent, gc->ngpio, name_len, GFP_KERNEL);
117 	if (!str)
118 		return -ENOMEM;
119 
120 	/* Get gpio line names one at a time */
121 	for (i = 0; i < gc->ngpio; i++) {
122 		params.get_info.index = i;
123 		ret = cros_ec_cmd(cros_ec, 1, EC_CMD_GPIO_GET, &params,
124 				  sizeof(params), &response, sizeof(response));
125 		if (ret < 0) {
126 			dev_err_probe(gc->parent, ret, "error getting gpio%d info\n", i);
127 			return ret;
128 		}
129 
130 		names[i] = str;
131 		copied = scnprintf(str, name_len, "%s%s", cros_ec_gpio_prefix,
132 				   response.get_info.name);
133 		if (copied < 0)
134 			return copied;
135 
136 		str += copied + 1;
137 	}
138 
139 	return 0;
140 }
141 
142 /* Query EC for number of gpios */
cros_ec_gpio_ngpios(struct cros_ec_device * cros_ec)143 static int cros_ec_gpio_ngpios(struct cros_ec_device *cros_ec)
144 {
145 	struct ec_params_gpio_get_v1 params = {
146 		.subcmd = EC_GPIO_GET_COUNT,
147 	};
148 	struct ec_response_gpio_get_v1 response;
149 	int ret;
150 
151 	ret = cros_ec_cmd(cros_ec, 1, EC_CMD_GPIO_GET, &params,
152 			  sizeof(params), &response, sizeof(response));
153 	if (ret < 0)
154 		return ret;
155 
156 	return response.get_count.val;
157 }
158 
cros_ec_gpio_probe(struct platform_device * pdev)159 static int cros_ec_gpio_probe(struct platform_device *pdev)
160 {
161 	struct device *dev = &pdev->dev;
162 	struct device *parent = dev->parent;
163 	struct cros_ec_dev *ec_dev = dev_get_drvdata(parent);
164 	struct cros_ec_device *cros_ec = ec_dev->ec_dev;
165 	struct gpio_chip *gc;
166 	int ngpios;
167 	int ret;
168 
169 	/* Use the fwnode from the protocol device, e.g. cros-ec-spi */
170 	device_set_node(dev, dev_fwnode(cros_ec->dev));
171 
172 	ngpios = cros_ec_gpio_ngpios(cros_ec);
173 	if (ngpios < 0) {
174 		dev_err_probe(dev, ngpios, "error getting gpio count\n");
175 		return ngpios;
176 	}
177 
178 	gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL);
179 	if (!gc)
180 		return -ENOMEM;
181 
182 	gc->ngpio = ngpios;
183 	gc->parent = dev;
184 	ret = cros_ec_gpio_init_names(cros_ec, gc);
185 	if (ret)
186 		return ret;
187 
188 	gc->can_sleep = true;
189 	gc->label = dev_name(dev);
190 	gc->base = -1;
191 	gc->set_rv = cros_ec_gpio_set;
192 	gc->get = cros_ec_gpio_get;
193 	gc->get_direction = cros_ec_gpio_get_direction;
194 
195 	return devm_gpiochip_add_data(dev, gc, cros_ec);
196 }
197 
198 static const struct platform_device_id cros_ec_gpio_id[] = {
199 	{ "cros-ec-gpio", 0 },
200 	{}
201 };
202 MODULE_DEVICE_TABLE(platform, cros_ec_gpio_id);
203 
204 static struct platform_driver cros_ec_gpio_driver = {
205 	.probe = cros_ec_gpio_probe,
206 	.driver = {
207 		.name = "cros-ec-gpio",
208 	},
209 	.id_table = cros_ec_gpio_id,
210 };
211 module_platform_driver(cros_ec_gpio_driver);
212 
213 MODULE_DESCRIPTION("ChromeOS EC GPIO Driver");
214 MODULE_LICENSE("GPL");
215