blob: 869dc952cf45218b7cc7c295f31ea730aaab836a [file] [log] [blame]
Geert Uytterhoeven828546e2020-05-11 16:52:55 +02001// SPDX-License-Identifier: GPL-2.0-only
2//
3// GPIO Aggregator
4//
5// Copyright (C) 2019-2020 Glider bv
6
7#define DRV_NAME "gpio-aggregator"
8#define pr_fmt(fmt) DRV_NAME ": " fmt
9
10#include <linux/bitmap.h>
11#include <linux/bitops.h>
12#include <linux/ctype.h>
Geert Uytterhoevenec750392020-07-01 13:42:12 +020013#include <linux/gpio.h>
Geert Uytterhoeven828546e2020-05-11 16:52:55 +020014#include <linux/gpio/consumer.h>
15#include <linux/gpio/driver.h>
16#include <linux/gpio/machine.h>
17#include <linux/idr.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/mutex.h>
21#include <linux/overflow.h>
22#include <linux/platform_device.h>
23#include <linux/spinlock.h>
24#include <linux/string.h>
25
26
27/*
28 * GPIO Aggregator sysfs interface
29 */
30
31struct gpio_aggregator {
32 struct gpiod_lookup_table *lookups;
33 struct platform_device *pdev;
34 char args[];
35};
36
37static DEFINE_MUTEX(gpio_aggregator_lock); /* protects idr */
38static DEFINE_IDR(gpio_aggregator_idr);
39
Geert Uytterhoeven828546e2020-05-11 16:52:55 +020040static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,
41 int hwnum, unsigned int *n)
42{
43 struct gpiod_lookup_table *lookups;
44
45 lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2),
46 GFP_KERNEL);
47 if (!lookups)
48 return -ENOMEM;
49
Andy Shevchenkob2498cb2021-01-22 14:38:52 +020050 lookups->table[*n] = GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0);
Geert Uytterhoeven828546e2020-05-11 16:52:55 +020051
52 (*n)++;
53 memset(&lookups->table[*n], 0, sizeof(lookups->table[*n]));
54
55 aggr->lookups = lookups;
56 return 0;
57}
58
59static int aggr_parse(struct gpio_aggregator *aggr)
60{
Andy Shevchenkoac505b62021-03-01 18:59:32 +020061 char *args = skip_spaces(aggr->args);
Andy Shevchenkodeb631c2021-01-22 14:38:51 +020062 char *name, *offsets, *p;
Geert Uytterhoevenec750392020-07-01 13:42:12 +020063 unsigned long *bitmap;
64 unsigned int i, n = 0;
Geert Uytterhoevenec750392020-07-01 13:42:12 +020065 int error = 0;
66
67 bitmap = bitmap_alloc(ARCH_NR_GPIOS, GFP_KERNEL);
68 if (!bitmap)
69 return -ENOMEM;
Geert Uytterhoeven828546e2020-05-11 16:52:55 +020070
Andy Shevchenkoac505b62021-03-01 18:59:32 +020071 args = next_arg(args, &name, &p);
72 while (*args) {
73 args = next_arg(args, &offsets, &p);
Geert Uytterhoeven828546e2020-05-11 16:52:55 +020074
Andy Shevchenkodeb631c2021-01-22 14:38:51 +020075 p = get_options(offsets, 0, &error);
76 if (error == 0 || *p) {
Geert Uytterhoeven828546e2020-05-11 16:52:55 +020077 /* Named GPIO line */
78 error = aggr_add_gpio(aggr, name, U16_MAX, &n);
79 if (error)
Geert Uytterhoevenec750392020-07-01 13:42:12 +020080 goto free_bitmap;
Geert Uytterhoeven828546e2020-05-11 16:52:55 +020081
82 name = offsets;
83 continue;
84 }
85
86 /* GPIO chip + offset(s) */
Geert Uytterhoevenec750392020-07-01 13:42:12 +020087 error = bitmap_parselist(offsets, bitmap, ARCH_NR_GPIOS);
88 if (error) {
89 pr_err("Cannot parse %s: %d\n", offsets, error);
90 goto free_bitmap;
91 }
Geert Uytterhoeven828546e2020-05-11 16:52:55 +020092
Geert Uytterhoevenec750392020-07-01 13:42:12 +020093 for_each_set_bit(i, bitmap, ARCH_NR_GPIOS) {
94 error = aggr_add_gpio(aggr, name, i, &n);
95 if (error)
96 goto free_bitmap;
Geert Uytterhoeven828546e2020-05-11 16:52:55 +020097 }
98
Andy Shevchenkoac505b62021-03-01 18:59:32 +020099 args = next_arg(args, &name, &p);
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200100 }
101
102 if (!n) {
103 pr_err("No GPIOs specified\n");
Geert Uytterhoevenec750392020-07-01 13:42:12 +0200104 error = -EINVAL;
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200105 }
106
Geert Uytterhoevenec750392020-07-01 13:42:12 +0200107free_bitmap:
108 bitmap_free(bitmap);
109 return error;
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200110}
111
112static ssize_t new_device_store(struct device_driver *driver, const char *buf,
113 size_t count)
114{
115 struct gpio_aggregator *aggr;
116 struct platform_device *pdev;
117 int res, id;
118
119 /* kernfs guarantees string termination, so count + 1 is safe */
120 aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL);
121 if (!aggr)
122 return -ENOMEM;
123
124 memcpy(aggr->args, buf, count + 1);
125
126 aggr->lookups = kzalloc(struct_size(aggr->lookups, table, 1),
127 GFP_KERNEL);
128 if (!aggr->lookups) {
129 res = -ENOMEM;
130 goto free_ga;
131 }
132
133 mutex_lock(&gpio_aggregator_lock);
134 id = idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL);
135 mutex_unlock(&gpio_aggregator_lock);
136
137 if (id < 0) {
138 res = id;
139 goto free_table;
140 }
141
142 aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id);
143 if (!aggr->lookups->dev_id) {
144 res = -ENOMEM;
145 goto remove_idr;
146 }
147
148 res = aggr_parse(aggr);
149 if (res)
150 goto free_dev_id;
151
152 gpiod_add_lookup_table(aggr->lookups);
153
154 pdev = platform_device_register_simple(DRV_NAME, id, NULL, 0);
155 if (IS_ERR(pdev)) {
156 res = PTR_ERR(pdev);
157 goto remove_table;
158 }
159
160 aggr->pdev = pdev;
161 return count;
162
163remove_table:
164 gpiod_remove_lookup_table(aggr->lookups);
165free_dev_id:
166 kfree(aggr->lookups->dev_id);
167remove_idr:
168 mutex_lock(&gpio_aggregator_lock);
169 idr_remove(&gpio_aggregator_idr, id);
170 mutex_unlock(&gpio_aggregator_lock);
171free_table:
172 kfree(aggr->lookups);
173free_ga:
174 kfree(aggr);
175 return res;
176}
177
178static DRIVER_ATTR_WO(new_device);
179
180static void gpio_aggregator_free(struct gpio_aggregator *aggr)
181{
182 platform_device_unregister(aggr->pdev);
183 gpiod_remove_lookup_table(aggr->lookups);
184 kfree(aggr->lookups->dev_id);
185 kfree(aggr->lookups);
186 kfree(aggr);
187}
188
189static ssize_t delete_device_store(struct device_driver *driver,
190 const char *buf, size_t count)
191{
192 struct gpio_aggregator *aggr;
193 unsigned int id;
194 int error;
195
196 if (!str_has_prefix(buf, DRV_NAME "."))
197 return -EINVAL;
198
199 error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id);
200 if (error)
201 return error;
202
203 mutex_lock(&gpio_aggregator_lock);
204 aggr = idr_remove(&gpio_aggregator_idr, id);
205 mutex_unlock(&gpio_aggregator_lock);
206 if (!aggr)
207 return -ENOENT;
208
209 gpio_aggregator_free(aggr);
210 return count;
211}
212static DRIVER_ATTR_WO(delete_device);
213
214static struct attribute *gpio_aggregator_attrs[] = {
215 &driver_attr_new_device.attr,
216 &driver_attr_delete_device.attr,
Andy Shevchenko6e004a92021-01-22 14:38:53 +0200217 NULL
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200218};
219ATTRIBUTE_GROUPS(gpio_aggregator);
220
221static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data)
222{
223 gpio_aggregator_free(p);
224 return 0;
225}
226
227static void __exit gpio_aggregator_remove_all(void)
228{
229 mutex_lock(&gpio_aggregator_lock);
230 idr_for_each(&gpio_aggregator_idr, gpio_aggregator_idr_remove, NULL);
231 idr_destroy(&gpio_aggregator_idr);
232 mutex_unlock(&gpio_aggregator_lock);
233}
234
235
236/*
237 * GPIO Forwarder
238 */
239
240struct gpiochip_fwd {
241 struct gpio_chip chip;
242 struct gpio_desc **descs;
243 union {
244 struct mutex mlock; /* protects tmp[] if can_sleep */
245 spinlock_t slock; /* protects tmp[] if !can_sleep */
246 };
247 unsigned long tmp[]; /* values and descs for multiple ops */
248};
249
Geert Uytterhoevenaa4858e2021-10-04 14:45:45 +0200250#define fwd_tmp_values(fwd) &(fwd)->tmp[0]
251#define fwd_tmp_descs(fwd) (void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
252
253#define fwd_tmp_size(ngpios) (BITS_TO_LONGS((ngpios)) + (ngpios))
254
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200255static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
256{
257 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
258
259 return gpiod_get_direction(fwd->descs[offset]);
260}
261
262static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset)
263{
264 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
265
266 return gpiod_direction_input(fwd->descs[offset]);
267}
268
269static int gpio_fwd_direction_output(struct gpio_chip *chip,
270 unsigned int offset, int value)
271{
272 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
273
274 return gpiod_direction_output(fwd->descs[offset], value);
275}
276
277static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
278{
279 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
280
281 return gpiod_get_value(fwd->descs[offset]);
282}
283
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300284static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200285 unsigned long *bits)
286{
Geert Uytterhoevenaa4858e2021-10-04 14:45:45 +0200287 struct gpio_desc **descs = fwd_tmp_descs(fwd);
288 unsigned long *values = fwd_tmp_values(fwd);
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200289 unsigned int i, j = 0;
290 int error;
291
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200292 bitmap_clear(values, 0, fwd->chip.ngpio);
293 for_each_set_bit(i, mask, fwd->chip.ngpio)
294 descs[j++] = fwd->descs[i];
295
296 error = gpiod_get_array_value(j, descs, NULL, values);
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300297 if (error)
298 return error;
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200299
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300300 j = 0;
301 for_each_set_bit(i, mask, fwd->chip.ngpio)
302 __assign_bit(i, bits, test_bit(j++, values));
303
304 return 0;
305}
306
307static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
308 unsigned long *mask, unsigned long *bits)
309{
310 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
311 unsigned long flags;
312 int error;
313
314 if (chip->can_sleep) {
315 mutex_lock(&fwd->mlock);
316 error = gpio_fwd_get_multiple(fwd, mask, bits);
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200317 mutex_unlock(&fwd->mlock);
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300318 } else {
319 spin_lock_irqsave(&fwd->slock, flags);
320 error = gpio_fwd_get_multiple(fwd, mask, bits);
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200321 spin_unlock_irqrestore(&fwd->slock, flags);
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300322 }
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200323
324 return error;
325}
326
327static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
328{
329 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
330
331 gpiod_set_value(fwd->descs[offset], value);
332}
333
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300334static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200335 unsigned long *bits)
336{
Geert Uytterhoevenaa4858e2021-10-04 14:45:45 +0200337 struct gpio_desc **descs = fwd_tmp_descs(fwd);
338 unsigned long *values = fwd_tmp_values(fwd);
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200339 unsigned int i, j = 0;
340
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200341 for_each_set_bit(i, mask, fwd->chip.ngpio) {
342 __assign_bit(j, values, test_bit(i, bits));
343 descs[j++] = fwd->descs[i];
344 }
345
346 gpiod_set_array_value(j, descs, NULL, values);
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300347}
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200348
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300349static void gpio_fwd_set_multiple_locked(struct gpio_chip *chip,
350 unsigned long *mask, unsigned long *bits)
351{
352 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
353 unsigned long flags;
354
355 if (chip->can_sleep) {
356 mutex_lock(&fwd->mlock);
357 gpio_fwd_set_multiple(fwd, mask, bits);
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200358 mutex_unlock(&fwd->mlock);
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300359 } else {
360 spin_lock_irqsave(&fwd->slock, flags);
361 gpio_fwd_set_multiple(fwd, mask, bits);
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200362 spin_unlock_irqrestore(&fwd->slock, flags);
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300363 }
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200364}
365
366static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
367 unsigned long config)
368{
369 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
370
371 return gpiod_set_config(fwd->descs[offset], config);
372}
373
Geert Uytterhoevena00128d2021-12-03 15:06:44 +0100374static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset)
375{
376 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
377
378 return gpiod_to_irq(fwd->descs[offset]);
379}
380
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200381/**
382 * gpiochip_fwd_create() - Create a new GPIO forwarder
383 * @dev: Parent device pointer
384 * @ngpios: Number of GPIOs in the forwarder.
385 * @descs: Array containing the GPIO descriptors to forward to.
386 * This array must contain @ngpios entries, and must not be deallocated
387 * before the forwarder has been destroyed again.
388 *
389 * This function creates a new gpiochip, which forwards all GPIO operations to
390 * the passed GPIO descriptors.
391 *
392 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
393 * code on failure.
394 */
395static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
396 unsigned int ngpios,
397 struct gpio_desc *descs[])
398{
399 const char *label = dev_name(dev);
400 struct gpiochip_fwd *fwd;
401 struct gpio_chip *chip;
402 unsigned int i;
403 int error;
404
Geert Uytterhoevenaa4858e2021-10-04 14:45:45 +0200405 fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
406 GFP_KERNEL);
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200407 if (!fwd)
408 return ERR_PTR(-ENOMEM);
409
410 chip = &fwd->chip;
411
412 /*
413 * If any of the GPIO lines are sleeping, then the entire forwarder
414 * will be sleeping.
415 * If any of the chips support .set_config(), then the forwarder will
416 * support setting configs.
417 */
418 for (i = 0; i < ngpios; i++) {
419 struct gpio_chip *parent = gpiod_to_chip(descs[i]);
420
Geert Uytterhoevena00128d2021-12-03 15:06:44 +0100421 dev_dbg(dev, "%u => gpio %d irq %d\n", i,
422 desc_to_gpio(descs[i]), gpiod_to_irq(descs[i]));
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200423
424 if (gpiod_cansleep(descs[i]))
425 chip->can_sleep = true;
426 if (parent && parent->set_config)
427 chip->set_config = gpio_fwd_set_config;
428 }
429
430 chip->label = label;
431 chip->parent = dev;
432 chip->owner = THIS_MODULE;
433 chip->get_direction = gpio_fwd_get_direction;
434 chip->direction_input = gpio_fwd_direction_input;
435 chip->direction_output = gpio_fwd_direction_output;
436 chip->get = gpio_fwd_get;
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300437 chip->get_multiple = gpio_fwd_get_multiple_locked;
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200438 chip->set = gpio_fwd_set;
Andy Shevchenko01e8d852020-08-10 14:43:53 +0300439 chip->set_multiple = gpio_fwd_set_multiple_locked;
Geert Uytterhoevena00128d2021-12-03 15:06:44 +0100440 chip->to_irq = gpio_fwd_to_irq;
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200441 chip->base = -1;
442 chip->ngpio = ngpios;
443 fwd->descs = descs;
444
445 if (chip->can_sleep)
446 mutex_init(&fwd->mlock);
447 else
448 spin_lock_init(&fwd->slock);
449
450 error = devm_gpiochip_add_data(dev, chip, fwd);
451 if (error)
452 return ERR_PTR(error);
453
454 return fwd;
455}
456
457
458/*
459 * GPIO Aggregator platform device
460 */
461
462static int gpio_aggregator_probe(struct platform_device *pdev)
463{
464 struct device *dev = &pdev->dev;
465 struct gpio_desc **descs;
466 struct gpiochip_fwd *fwd;
467 int i, n;
468
469 n = gpiod_count(dev, NULL);
470 if (n < 0)
471 return n;
472
473 descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL);
474 if (!descs)
475 return -ENOMEM;
476
477 for (i = 0; i < n; i++) {
478 descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
479 if (IS_ERR(descs[i]))
480 return PTR_ERR(descs[i]);
481 }
482
483 fwd = gpiochip_fwd_create(dev, n, descs);
484 if (IS_ERR(fwd))
485 return PTR_ERR(fwd);
486
487 platform_set_drvdata(pdev, fwd);
488 return 0;
489}
490
491#ifdef CONFIG_OF
492static const struct of_device_id gpio_aggregator_dt_ids[] = {
493 /*
494 * Add GPIO-operated devices controlled from userspace below,
495 * or use "driver_override" in sysfs
496 */
Andy Shevchenko6e004a92021-01-22 14:38:53 +0200497 {}
Geert Uytterhoeven828546e2020-05-11 16:52:55 +0200498};
499MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
500#endif
501
502static struct platform_driver gpio_aggregator_driver = {
503 .probe = gpio_aggregator_probe,
504 .driver = {
505 .name = DRV_NAME,
506 .groups = gpio_aggregator_groups,
507 .of_match_table = of_match_ptr(gpio_aggregator_dt_ids),
508 },
509};
510
511static int __init gpio_aggregator_init(void)
512{
513 return platform_driver_register(&gpio_aggregator_driver);
514}
515module_init(gpio_aggregator_init);
516
517static void __exit gpio_aggregator_exit(void)
518{
519 gpio_aggregator_remove_all();
520 platform_driver_unregister(&gpio_aggregator_driver);
521}
522module_exit(gpio_aggregator_exit);
523
524MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
525MODULE_DESCRIPTION("GPIO Aggregator");
526MODULE_LICENSE("GPL v2");