1 // SPDX-License-Identifier: GPL-2.0-only
5 // Copyright (C) 2019-2020 Glider bv
7 #define DRV_NAME "gpio-aggregator"
8 #define pr_fmt(fmt) DRV_NAME ": " fmt
10 #include <linux/bitmap.h>
11 #include <linux/bitops.h>
12 #include <linux/ctype.h>
13 #include <linux/gpio.h>
14 #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>
28 * GPIO Aggregator sysfs interface
31 struct gpio_aggregator {
32 struct gpiod_lookup_table *lookups;
33 struct platform_device *pdev;
37 static DEFINE_MUTEX(gpio_aggregator_lock); /* protects idr */
38 static DEFINE_IDR(gpio_aggregator_idr);
40 static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,
41 int hwnum, unsigned int *n)
43 struct gpiod_lookup_table *lookups;
45 lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2),
50 lookups->table[*n] = GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0);
53 memset(&lookups->table[*n], 0, sizeof(lookups->table[*n]));
55 aggr->lookups = lookups;
59 static int aggr_parse(struct gpio_aggregator *aggr)
61 char *args = skip_spaces(aggr->args);
62 char *name, *offsets, *p;
63 unsigned long *bitmap;
64 unsigned int i, n = 0;
67 bitmap = bitmap_alloc(ARCH_NR_GPIOS, GFP_KERNEL);
71 args = next_arg(args, &name, &p);
73 args = next_arg(args, &offsets, &p);
75 p = get_options(offsets, 0, &error);
76 if (error == 0 || *p) {
78 error = aggr_add_gpio(aggr, name, U16_MAX, &n);
86 /* GPIO chip + offset(s) */
87 error = bitmap_parselist(offsets, bitmap, ARCH_NR_GPIOS);
89 pr_err("Cannot parse %s: %d\n", offsets, error);
93 for_each_set_bit(i, bitmap, ARCH_NR_GPIOS) {
94 error = aggr_add_gpio(aggr, name, i, &n);
99 args = next_arg(args, &name, &p);
103 pr_err("No GPIOs specified\n");
112 static ssize_t new_device_store(struct device_driver *driver, const char *buf,
115 struct gpio_aggregator *aggr;
116 struct platform_device *pdev;
119 /* kernfs guarantees string termination, so count + 1 is safe */
120 aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL);
124 memcpy(aggr->args, buf, count + 1);
126 aggr->lookups = kzalloc(struct_size(aggr->lookups, table, 1),
128 if (!aggr->lookups) {
133 mutex_lock(&gpio_aggregator_lock);
134 id = idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL);
135 mutex_unlock(&gpio_aggregator_lock);
142 aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id);
143 if (!aggr->lookups->dev_id) {
148 res = aggr_parse(aggr);
152 gpiod_add_lookup_table(aggr->lookups);
154 pdev = platform_device_register_simple(DRV_NAME, id, NULL, 0);
164 gpiod_remove_lookup_table(aggr->lookups);
166 kfree(aggr->lookups->dev_id);
168 mutex_lock(&gpio_aggregator_lock);
169 idr_remove(&gpio_aggregator_idr, id);
170 mutex_unlock(&gpio_aggregator_lock);
172 kfree(aggr->lookups);
178 static DRIVER_ATTR_WO(new_device);
180 static void gpio_aggregator_free(struct gpio_aggregator *aggr)
182 platform_device_unregister(aggr->pdev);
183 gpiod_remove_lookup_table(aggr->lookups);
184 kfree(aggr->lookups->dev_id);
185 kfree(aggr->lookups);
189 static ssize_t delete_device_store(struct device_driver *driver,
190 const char *buf, size_t count)
192 struct gpio_aggregator *aggr;
196 if (!str_has_prefix(buf, DRV_NAME "."))
199 error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id);
203 mutex_lock(&gpio_aggregator_lock);
204 aggr = idr_remove(&gpio_aggregator_idr, id);
205 mutex_unlock(&gpio_aggregator_lock);
209 gpio_aggregator_free(aggr);
212 static DRIVER_ATTR_WO(delete_device);
214 static struct attribute *gpio_aggregator_attrs[] = {
215 &driver_attr_new_device.attr,
216 &driver_attr_delete_device.attr,
219 ATTRIBUTE_GROUPS(gpio_aggregator);
221 static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data)
223 gpio_aggregator_free(p);
227 static void __exit gpio_aggregator_remove_all(void)
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);
240 struct gpiochip_fwd {
241 struct gpio_chip chip;
242 struct gpio_desc **descs;
244 struct mutex mlock; /* protects tmp[] if can_sleep */
245 spinlock_t slock; /* protects tmp[] if !can_sleep */
247 unsigned long tmp[]; /* values and descs for multiple ops */
250 static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
252 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
254 return gpiod_get_direction(fwd->descs[offset]);
257 static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset)
259 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
261 return gpiod_direction_input(fwd->descs[offset]);
264 static int gpio_fwd_direction_output(struct gpio_chip *chip,
265 unsigned int offset, int value)
267 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
269 return gpiod_direction_output(fwd->descs[offset], value);
272 static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
274 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
276 return chip->can_sleep ? gpiod_get_value_cansleep(fwd->descs[offset])
277 : gpiod_get_value(fwd->descs[offset]);
280 static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
283 struct gpio_desc **descs;
284 unsigned long *values;
285 unsigned int i, j = 0;
288 /* Both values bitmap and desc pointers are stored in tmp[] */
289 values = &fwd->tmp[0];
290 descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
292 bitmap_clear(values, 0, fwd->chip.ngpio);
293 for_each_set_bit(i, mask, fwd->chip.ngpio)
294 descs[j++] = fwd->descs[i];
296 if (fwd->chip.can_sleep)
297 error = gpiod_get_array_value_cansleep(j, descs, NULL, values);
299 error = gpiod_get_array_value(j, descs, NULL, values);
304 for_each_set_bit(i, mask, fwd->chip.ngpio)
305 __assign_bit(i, bits, test_bit(j++, values));
310 static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
311 unsigned long *mask, unsigned long *bits)
313 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
317 if (chip->can_sleep) {
318 mutex_lock(&fwd->mlock);
319 error = gpio_fwd_get_multiple(fwd, mask, bits);
320 mutex_unlock(&fwd->mlock);
322 spin_lock_irqsave(&fwd->slock, flags);
323 error = gpio_fwd_get_multiple(fwd, mask, bits);
324 spin_unlock_irqrestore(&fwd->slock, flags);
330 static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
332 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
335 gpiod_set_value_cansleep(fwd->descs[offset], value);
337 gpiod_set_value(fwd->descs[offset], value);
340 static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
343 struct gpio_desc **descs;
344 unsigned long *values;
345 unsigned int i, j = 0;
347 /* Both values bitmap and desc pointers are stored in tmp[] */
348 values = &fwd->tmp[0];
349 descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
351 for_each_set_bit(i, mask, fwd->chip.ngpio) {
352 __assign_bit(j, values, test_bit(i, bits));
353 descs[j++] = fwd->descs[i];
356 if (fwd->chip.can_sleep)
357 gpiod_set_array_value_cansleep(j, descs, NULL, values);
359 gpiod_set_array_value(j, descs, NULL, values);
362 static void gpio_fwd_set_multiple_locked(struct gpio_chip *chip,
363 unsigned long *mask, unsigned long *bits)
365 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
368 if (chip->can_sleep) {
369 mutex_lock(&fwd->mlock);
370 gpio_fwd_set_multiple(fwd, mask, bits);
371 mutex_unlock(&fwd->mlock);
373 spin_lock_irqsave(&fwd->slock, flags);
374 gpio_fwd_set_multiple(fwd, mask, bits);
375 spin_unlock_irqrestore(&fwd->slock, flags);
379 static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
380 unsigned long config)
382 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
384 return gpiod_set_config(fwd->descs[offset], config);
388 * gpiochip_fwd_create() - Create a new GPIO forwarder
389 * @dev: Parent device pointer
390 * @ngpios: Number of GPIOs in the forwarder.
391 * @descs: Array containing the GPIO descriptors to forward to.
392 * This array must contain @ngpios entries, and must not be deallocated
393 * before the forwarder has been destroyed again.
395 * This function creates a new gpiochip, which forwards all GPIO operations to
396 * the passed GPIO descriptors.
398 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
401 static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
403 struct gpio_desc *descs[])
405 const char *label = dev_name(dev);
406 struct gpiochip_fwd *fwd;
407 struct gpio_chip *chip;
411 fwd = devm_kzalloc(dev, struct_size(fwd, tmp,
412 BITS_TO_LONGS(ngpios) + ngpios), GFP_KERNEL);
414 return ERR_PTR(-ENOMEM);
419 * If any of the GPIO lines are sleeping, then the entire forwarder
421 * If any of the chips support .set_config(), then the forwarder will
422 * support setting configs.
424 for (i = 0; i < ngpios; i++) {
425 struct gpio_chip *parent = gpiod_to_chip(descs[i]);
427 dev_dbg(dev, "%u => gpio-%d\n", i, desc_to_gpio(descs[i]));
429 if (gpiod_cansleep(descs[i]))
430 chip->can_sleep = true;
431 if (parent && parent->set_config)
432 chip->set_config = gpio_fwd_set_config;
437 chip->owner = THIS_MODULE;
438 chip->get_direction = gpio_fwd_get_direction;
439 chip->direction_input = gpio_fwd_direction_input;
440 chip->direction_output = gpio_fwd_direction_output;
441 chip->get = gpio_fwd_get;
442 chip->get_multiple = gpio_fwd_get_multiple_locked;
443 chip->set = gpio_fwd_set;
444 chip->set_multiple = gpio_fwd_set_multiple_locked;
446 chip->ngpio = ngpios;
450 mutex_init(&fwd->mlock);
452 spin_lock_init(&fwd->slock);
454 error = devm_gpiochip_add_data(dev, chip, fwd);
456 return ERR_PTR(error);
463 * GPIO Aggregator platform device
466 static int gpio_aggregator_probe(struct platform_device *pdev)
468 struct device *dev = &pdev->dev;
469 struct gpio_desc **descs;
470 struct gpiochip_fwd *fwd;
473 n = gpiod_count(dev, NULL);
477 descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL);
481 for (i = 0; i < n; i++) {
482 descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
483 if (IS_ERR(descs[i]))
484 return PTR_ERR(descs[i]);
487 fwd = gpiochip_fwd_create(dev, n, descs);
491 platform_set_drvdata(pdev, fwd);
496 static const struct of_device_id gpio_aggregator_dt_ids[] = {
498 * Add GPIO-operated devices controlled from userspace below,
499 * or use "driver_override" in sysfs
503 MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
506 static struct platform_driver gpio_aggregator_driver = {
507 .probe = gpio_aggregator_probe,
510 .groups = gpio_aggregator_groups,
511 .of_match_table = of_match_ptr(gpio_aggregator_dt_ids),
515 static int __init gpio_aggregator_init(void)
517 return platform_driver_register(&gpio_aggregator_driver);
519 module_init(gpio_aggregator_init);
521 static void __exit gpio_aggregator_exit(void)
523 gpio_aggregator_remove_all();
524 platform_driver_unregister(&gpio_aggregator_driver);
526 module_exit(gpio_aggregator_exit);
528 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
529 MODULE_DESCRIPTION("GPIO Aggregator");
530 MODULE_LICENSE("GPL v2");