Merge branch 'misc.namei' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-microblaze.git] / drivers / gpio / gpio-virtio.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * GPIO driver for virtio-based virtual GPIO controllers
4  *
5  * Copyright (C) 2021 metux IT consult
6  * Enrico Weigelt, metux IT consult <info@metux.net>
7  *
8  * Copyright (C) 2021 Linaro.
9  * Viresh Kumar <viresh.kumar@linaro.org>
10  */
11
12 #include <linux/completion.h>
13 #include <linux/err.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/io.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/virtio_config.h>
20 #include <uapi/linux/virtio_gpio.h>
21 #include <uapi/linux/virtio_ids.h>
22
23 struct virtio_gpio_line {
24         struct mutex lock; /* Protects line operation */
25         struct completion completion;
26         struct virtio_gpio_request req ____cacheline_aligned;
27         struct virtio_gpio_response res ____cacheline_aligned;
28         unsigned int rxlen;
29 };
30
31 struct virtio_gpio {
32         struct virtio_device *vdev;
33         struct mutex lock; /* Protects virtqueue operation */
34         struct gpio_chip gc;
35         struct virtio_gpio_line *lines;
36         struct virtqueue *request_vq;
37 };
38
39 static int _virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
40                             u8 txvalue, u8 *rxvalue, void *response, u32 rxlen)
41 {
42         struct virtio_gpio_line *line = &vgpio->lines[gpio];
43         struct virtio_gpio_request *req = &line->req;
44         struct virtio_gpio_response *res = response;
45         struct scatterlist *sgs[2], req_sg, res_sg;
46         struct device *dev = &vgpio->vdev->dev;
47         int ret;
48
49         /*
50          * Prevent concurrent requests for the same line since we have
51          * pre-allocated request/response buffers for each GPIO line. Moreover
52          * Linux always accesses a GPIO line sequentially, so this locking shall
53          * always go through without any delays.
54          */
55         mutex_lock(&line->lock);
56
57         req->type = cpu_to_le16(type);
58         req->gpio = cpu_to_le16(gpio);
59         req->value = cpu_to_le32(txvalue);
60
61         sg_init_one(&req_sg, req, sizeof(*req));
62         sg_init_one(&res_sg, res, rxlen);
63         sgs[0] = &req_sg;
64         sgs[1] = &res_sg;
65
66         line->rxlen = 0;
67         reinit_completion(&line->completion);
68
69         /*
70          * Virtqueue callers need to ensure they don't call its APIs with other
71          * virtqueue operations at the same time.
72          */
73         mutex_lock(&vgpio->lock);
74         ret = virtqueue_add_sgs(vgpio->request_vq, sgs, 1, 1, line, GFP_KERNEL);
75         if (ret) {
76                 dev_err(dev, "failed to add request to vq\n");
77                 mutex_unlock(&vgpio->lock);
78                 goto out;
79         }
80
81         virtqueue_kick(vgpio->request_vq);
82         mutex_unlock(&vgpio->lock);
83
84         if (!wait_for_completion_timeout(&line->completion, HZ)) {
85                 dev_err(dev, "GPIO operation timed out\n");
86                 ret = -ETIMEDOUT;
87                 goto out;
88         }
89
90         if (unlikely(res->status != VIRTIO_GPIO_STATUS_OK)) {
91                 dev_err(dev, "GPIO request failed: %d\n", gpio);
92                 ret = -EINVAL;
93                 goto out;
94         }
95
96         if (unlikely(line->rxlen != rxlen)) {
97                 dev_err(dev, "GPIO operation returned incorrect len (%u : %u)\n",
98                         rxlen, line->rxlen);
99                 ret = -EINVAL;
100                 goto out;
101         }
102
103         if (rxvalue)
104                 *rxvalue = res->value;
105
106 out:
107         mutex_unlock(&line->lock);
108         return ret;
109 }
110
111 static int virtio_gpio_req(struct virtio_gpio *vgpio, u16 type, u16 gpio,
112                            u8 txvalue, u8 *rxvalue)
113 {
114         struct virtio_gpio_line *line = &vgpio->lines[gpio];
115         struct virtio_gpio_response *res = &line->res;
116
117         return _virtio_gpio_req(vgpio, type, gpio, txvalue, rxvalue, res,
118                                 sizeof(*res));
119 }
120
121 static void virtio_gpio_free(struct gpio_chip *gc, unsigned int gpio)
122 {
123         struct virtio_gpio *vgpio = gpiochip_get_data(gc);
124
125         virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
126                         VIRTIO_GPIO_DIRECTION_NONE, NULL);
127 }
128
129 static int virtio_gpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
130 {
131         struct virtio_gpio *vgpio = gpiochip_get_data(gc);
132         u8 direction;
133         int ret;
134
135         ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_DIRECTION, gpio, 0,
136                               &direction);
137         if (ret)
138                 return ret;
139
140         switch (direction) {
141         case VIRTIO_GPIO_DIRECTION_IN:
142                 return GPIO_LINE_DIRECTION_IN;
143         case VIRTIO_GPIO_DIRECTION_OUT:
144                 return GPIO_LINE_DIRECTION_OUT;
145         default:
146                 return -EINVAL;
147         }
148 }
149
150 static int virtio_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
151 {
152         struct virtio_gpio *vgpio = gpiochip_get_data(gc);
153
154         return virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
155                                VIRTIO_GPIO_DIRECTION_IN, NULL);
156 }
157
158 static int virtio_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
159                                         int value)
160 {
161         struct virtio_gpio *vgpio = gpiochip_get_data(gc);
162         int ret;
163
164         ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_VALUE, gpio, value, NULL);
165         if (ret)
166                 return ret;
167
168         return virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_DIRECTION, gpio,
169                                VIRTIO_GPIO_DIRECTION_OUT, NULL);
170 }
171
172 static int virtio_gpio_get(struct gpio_chip *gc, unsigned int gpio)
173 {
174         struct virtio_gpio *vgpio = gpiochip_get_data(gc);
175         u8 value;
176         int ret;
177
178         ret = virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_VALUE, gpio, 0, &value);
179         return ret ? ret : value;
180 }
181
182 static void virtio_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
183 {
184         struct virtio_gpio *vgpio = gpiochip_get_data(gc);
185
186         virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_SET_VALUE, gpio, value, NULL);
187 }
188
189 static void virtio_gpio_request_vq(struct virtqueue *vq)
190 {
191         struct virtio_gpio_line *line;
192         unsigned int len;
193
194         do {
195                 line = virtqueue_get_buf(vq, &len);
196                 if (!line)
197                         return;
198
199                 line->rxlen = len;
200                 complete(&line->completion);
201         } while (1);
202 }
203
204 static void virtio_gpio_free_vqs(struct virtio_device *vdev)
205 {
206         vdev->config->reset(vdev);
207         vdev->config->del_vqs(vdev);
208 }
209
210 static int virtio_gpio_alloc_vqs(struct virtio_gpio *vgpio,
211                                  struct virtio_device *vdev)
212 {
213         const char * const names[] = { "requestq" };
214         vq_callback_t *cbs[] = {
215                 virtio_gpio_request_vq,
216         };
217         struct virtqueue *vqs[1] = { NULL };
218         int ret;
219
220         ret = virtio_find_vqs(vdev, 1, vqs, cbs, names, NULL);
221         if (ret) {
222                 dev_err(&vdev->dev, "failed to find vqs: %d\n", ret);
223                 return ret;
224         }
225
226         if (!vqs[0]) {
227                 dev_err(&vdev->dev, "failed to find requestq vq\n");
228                 return -ENODEV;
229         }
230         vgpio->request_vq = vqs[0];
231
232         return 0;
233 }
234
235 static const char **virtio_gpio_get_names(struct virtio_gpio *vgpio,
236                                           u32 gpio_names_size, u16 ngpio)
237 {
238         struct virtio_gpio_response_get_names *res;
239         struct device *dev = &vgpio->vdev->dev;
240         u8 *gpio_names, *str;
241         const char **names;
242         int i, ret, len;
243
244         if (!gpio_names_size)
245                 return NULL;
246
247         len = sizeof(*res) + gpio_names_size;
248         res = devm_kzalloc(dev, len, GFP_KERNEL);
249         if (!res)
250                 return NULL;
251         gpio_names = res->value;
252
253         ret = _virtio_gpio_req(vgpio, VIRTIO_GPIO_MSG_GET_NAMES, 0, 0, NULL,
254                                res, len);
255         if (ret) {
256                 dev_err(dev, "Failed to get GPIO names: %d\n", ret);
257                 return NULL;
258         }
259
260         names = devm_kcalloc(dev, ngpio, sizeof(*names), GFP_KERNEL);
261         if (!names)
262                 return NULL;
263
264         /* NULL terminate the string instead of checking it */
265         gpio_names[gpio_names_size - 1] = '\0';
266
267         for (i = 0, str = gpio_names; i < ngpio; i++) {
268                 names[i] = str;
269                 str += strlen(str) + 1; /* zero-length strings are allowed */
270
271                 if (str > gpio_names + gpio_names_size) {
272                         dev_err(dev, "gpio_names block is too short (%d)\n", i);
273                         return NULL;
274                 }
275         }
276
277         return names;
278 }
279
280 static int virtio_gpio_probe(struct virtio_device *vdev)
281 {
282         struct virtio_gpio_config config;
283         struct device *dev = &vdev->dev;
284         struct virtio_gpio *vgpio;
285         u32 gpio_names_size;
286         u16 ngpio;
287         int ret, i;
288
289         vgpio = devm_kzalloc(dev, sizeof(*vgpio), GFP_KERNEL);
290         if (!vgpio)
291                 return -ENOMEM;
292
293         /* Read configuration */
294         virtio_cread_bytes(vdev, 0, &config, sizeof(config));
295         gpio_names_size = le32_to_cpu(config.gpio_names_size);
296         ngpio = le16_to_cpu(config.ngpio);
297         if (!ngpio) {
298                 dev_err(dev, "Number of GPIOs can't be zero\n");
299                 return -EINVAL;
300         }
301
302         vgpio->lines = devm_kcalloc(dev, ngpio, sizeof(*vgpio->lines), GFP_KERNEL);
303         if (!vgpio->lines)
304                 return -ENOMEM;
305
306         for (i = 0; i < ngpio; i++) {
307                 mutex_init(&vgpio->lines[i].lock);
308                 init_completion(&vgpio->lines[i].completion);
309         }
310
311         mutex_init(&vgpio->lock);
312         vdev->priv = vgpio;
313
314         vgpio->vdev                     = vdev;
315         vgpio->gc.free                  = virtio_gpio_free;
316         vgpio->gc.get_direction         = virtio_gpio_get_direction;
317         vgpio->gc.direction_input       = virtio_gpio_direction_input;
318         vgpio->gc.direction_output      = virtio_gpio_direction_output;
319         vgpio->gc.get                   = virtio_gpio_get;
320         vgpio->gc.set                   = virtio_gpio_set;
321         vgpio->gc.ngpio                 = ngpio;
322         vgpio->gc.base                  = -1; /* Allocate base dynamically */
323         vgpio->gc.label                 = dev_name(dev);
324         vgpio->gc.parent                = dev;
325         vgpio->gc.owner                 = THIS_MODULE;
326         vgpio->gc.can_sleep             = true;
327
328         ret = virtio_gpio_alloc_vqs(vgpio, vdev);
329         if (ret)
330                 return ret;
331
332         /* Mark the device ready to perform operations from within probe() */
333         virtio_device_ready(vdev);
334
335         vgpio->gc.names = virtio_gpio_get_names(vgpio, gpio_names_size, ngpio);
336
337         ret = gpiochip_add_data(&vgpio->gc, vgpio);
338         if (ret) {
339                 virtio_gpio_free_vqs(vdev);
340                 dev_err(dev, "Failed to add virtio-gpio controller\n");
341         }
342
343         return ret;
344 }
345
346 static void virtio_gpio_remove(struct virtio_device *vdev)
347 {
348         struct virtio_gpio *vgpio = vdev->priv;
349
350         gpiochip_remove(&vgpio->gc);
351         virtio_gpio_free_vqs(vdev);
352 }
353
354 static const struct virtio_device_id id_table[] = {
355         { VIRTIO_ID_GPIO, VIRTIO_DEV_ANY_ID },
356         {},
357 };
358 MODULE_DEVICE_TABLE(virtio, id_table);
359
360 static struct virtio_driver virtio_gpio_driver = {
361         .id_table               = id_table,
362         .probe                  = virtio_gpio_probe,
363         .remove                 = virtio_gpio_remove,
364         .driver                 = {
365                 .name           = KBUILD_MODNAME,
366                 .owner          = THIS_MODULE,
367         },
368 };
369 module_virtio_driver(virtio_gpio_driver);
370
371 MODULE_AUTHOR("Enrico Weigelt, metux IT consult <info@metux.net>");
372 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
373 MODULE_DESCRIPTION("VirtIO GPIO driver");
374 MODULE_LICENSE("GPL");