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/consumer.h>
14 #include <linux/gpio/driver.h>
15 #include <linux/gpio/machine.h>
16 #include <linux/idr.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/overflow.h>
21 #include <linux/platform_device.h>
22 #include <linux/spinlock.h>
23 #include <linux/string.h>
27 * GPIO Aggregator sysfs interface
30 struct gpio_aggregator {
31 struct gpiod_lookup_table *lookups;
32 struct platform_device *pdev;
36 static DEFINE_MUTEX(gpio_aggregator_lock); /* protects idr */
37 static DEFINE_IDR(gpio_aggregator_idr);
39 static char *get_arg(char **args)
41 char *start = *args, *end;
43 start = skip_spaces(start);
49 end = strchr(++start, '"');
51 return ERR_PTR(-EINVAL);
54 for (end = start; *end && !isspace(*end); end++) ;
64 static bool isrange(const char *s)
68 if (IS_ERR_OR_NULL(s))
72 n = strspn(s, "0123456789");
92 static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,
93 int hwnum, unsigned int *n)
95 struct gpiod_lookup_table *lookups;
97 lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2),
103 (struct gpiod_lookup)GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0);
106 memset(&lookups->table[*n], 0, sizeof(lookups->table[*n]));
108 aggr->lookups = lookups;
112 static int aggr_parse(struct gpio_aggregator *aggr)
114 unsigned int first_index, last_index, i, n = 0;
115 char *name, *offsets, *first, *last, *next;
116 char *args = aggr->args;
119 for (name = get_arg(&args), offsets = get_arg(&args); name;
120 offsets = get_arg(&args)) {
122 pr_err("Cannot get GPIO specifier: %pe\n", name);
123 return PTR_ERR(name);
126 if (!isrange(offsets)) {
127 /* Named GPIO line */
128 error = aggr_add_gpio(aggr, name, U16_MAX, &n);
136 /* GPIO chip + offset(s) */
137 for (first = offsets; *first; first = next) {
138 next = strchrnul(first, ',');
142 last = strchr(first, '-');
146 if (kstrtouint(first, 10, &first_index)) {
147 pr_err("Cannot parse GPIO index %s\n", first);
152 last_index = first_index;
153 } else if (kstrtouint(last, 10, &last_index)) {
154 pr_err("Cannot parse GPIO index %s\n", last);
158 for (i = first_index; i <= last_index; i++) {
159 error = aggr_add_gpio(aggr, name, i, &n);
165 name = get_arg(&args);
169 pr_err("No GPIOs specified\n");
176 static ssize_t new_device_store(struct device_driver *driver, const char *buf,
179 struct gpio_aggregator *aggr;
180 struct platform_device *pdev;
183 /* kernfs guarantees string termination, so count + 1 is safe */
184 aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL);
188 memcpy(aggr->args, buf, count + 1);
190 aggr->lookups = kzalloc(struct_size(aggr->lookups, table, 1),
192 if (!aggr->lookups) {
197 mutex_lock(&gpio_aggregator_lock);
198 id = idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL);
199 mutex_unlock(&gpio_aggregator_lock);
206 aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id);
207 if (!aggr->lookups->dev_id) {
212 res = aggr_parse(aggr);
216 gpiod_add_lookup_table(aggr->lookups);
218 pdev = platform_device_register_simple(DRV_NAME, id, NULL, 0);
228 gpiod_remove_lookup_table(aggr->lookups);
230 kfree(aggr->lookups->dev_id);
232 mutex_lock(&gpio_aggregator_lock);
233 idr_remove(&gpio_aggregator_idr, id);
234 mutex_unlock(&gpio_aggregator_lock);
236 kfree(aggr->lookups);
242 static DRIVER_ATTR_WO(new_device);
244 static void gpio_aggregator_free(struct gpio_aggregator *aggr)
246 platform_device_unregister(aggr->pdev);
247 gpiod_remove_lookup_table(aggr->lookups);
248 kfree(aggr->lookups->dev_id);
249 kfree(aggr->lookups);
253 static ssize_t delete_device_store(struct device_driver *driver,
254 const char *buf, size_t count)
256 struct gpio_aggregator *aggr;
260 if (!str_has_prefix(buf, DRV_NAME "."))
263 error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id);
267 mutex_lock(&gpio_aggregator_lock);
268 aggr = idr_remove(&gpio_aggregator_idr, id);
269 mutex_unlock(&gpio_aggregator_lock);
273 gpio_aggregator_free(aggr);
276 static DRIVER_ATTR_WO(delete_device);
278 static struct attribute *gpio_aggregator_attrs[] = {
279 &driver_attr_new_device.attr,
280 &driver_attr_delete_device.attr,
283 ATTRIBUTE_GROUPS(gpio_aggregator);
285 static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data)
287 gpio_aggregator_free(p);
291 static void __exit gpio_aggregator_remove_all(void)
293 mutex_lock(&gpio_aggregator_lock);
294 idr_for_each(&gpio_aggregator_idr, gpio_aggregator_idr_remove, NULL);
295 idr_destroy(&gpio_aggregator_idr);
296 mutex_unlock(&gpio_aggregator_lock);
304 struct gpiochip_fwd {
305 struct gpio_chip chip;
306 struct gpio_desc **descs;
308 struct mutex mlock; /* protects tmp[] if can_sleep */
309 spinlock_t slock; /* protects tmp[] if !can_sleep */
311 unsigned long tmp[]; /* values and descs for multiple ops */
314 static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
316 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
318 return gpiod_get_direction(fwd->descs[offset]);
321 static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset)
323 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
325 return gpiod_direction_input(fwd->descs[offset]);
328 static int gpio_fwd_direction_output(struct gpio_chip *chip,
329 unsigned int offset, int value)
331 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
333 return gpiod_direction_output(fwd->descs[offset], value);
336 static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
338 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
340 return gpiod_get_value(fwd->descs[offset]);
343 static int gpio_fwd_get_multiple(struct gpio_chip *chip, unsigned long *mask,
346 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
347 unsigned long *values, flags = 0;
348 struct gpio_desc **descs;
349 unsigned int i, j = 0;
353 mutex_lock(&fwd->mlock);
355 spin_lock_irqsave(&fwd->slock, flags);
357 /* Both values bitmap and desc pointers are stored in tmp[] */
358 values = &fwd->tmp[0];
359 descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
361 bitmap_clear(values, 0, fwd->chip.ngpio);
362 for_each_set_bit(i, mask, fwd->chip.ngpio)
363 descs[j++] = fwd->descs[i];
365 error = gpiod_get_array_value(j, descs, NULL, values);
368 for_each_set_bit(i, mask, fwd->chip.ngpio)
369 __assign_bit(i, bits, test_bit(j++, values));
373 mutex_unlock(&fwd->mlock);
375 spin_unlock_irqrestore(&fwd->slock, flags);
380 static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
382 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
384 gpiod_set_value(fwd->descs[offset], value);
387 static void gpio_fwd_set_multiple(struct gpio_chip *chip, unsigned long *mask,
390 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
391 unsigned long *values, flags = 0;
392 struct gpio_desc **descs;
393 unsigned int i, j = 0;
396 mutex_lock(&fwd->mlock);
398 spin_lock_irqsave(&fwd->slock, flags);
400 /* Both values bitmap and desc pointers are stored in tmp[] */
401 values = &fwd->tmp[0];
402 descs = (void *)&fwd->tmp[BITS_TO_LONGS(fwd->chip.ngpio)];
404 for_each_set_bit(i, mask, fwd->chip.ngpio) {
405 __assign_bit(j, values, test_bit(i, bits));
406 descs[j++] = fwd->descs[i];
409 gpiod_set_array_value(j, descs, NULL, values);
412 mutex_unlock(&fwd->mlock);
414 spin_unlock_irqrestore(&fwd->slock, flags);
417 static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
418 unsigned long config)
420 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
422 return gpiod_set_config(fwd->descs[offset], config);
426 * gpiochip_fwd_create() - Create a new GPIO forwarder
427 * @dev: Parent device pointer
428 * @ngpios: Number of GPIOs in the forwarder.
429 * @descs: Array containing the GPIO descriptors to forward to.
430 * This array must contain @ngpios entries, and must not be deallocated
431 * before the forwarder has been destroyed again.
433 * This function creates a new gpiochip, which forwards all GPIO operations to
434 * the passed GPIO descriptors.
436 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
439 static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
441 struct gpio_desc *descs[])
443 const char *label = dev_name(dev);
444 struct gpiochip_fwd *fwd;
445 struct gpio_chip *chip;
449 fwd = devm_kzalloc(dev, struct_size(fwd, tmp,
450 BITS_TO_LONGS(ngpios) + ngpios), GFP_KERNEL);
452 return ERR_PTR(-ENOMEM);
457 * If any of the GPIO lines are sleeping, then the entire forwarder
459 * If any of the chips support .set_config(), then the forwarder will
460 * support setting configs.
462 for (i = 0; i < ngpios; i++) {
463 struct gpio_chip *parent = gpiod_to_chip(descs[i]);
465 dev_dbg(dev, "%u => gpio-%d\n", i, desc_to_gpio(descs[i]));
467 if (gpiod_cansleep(descs[i]))
468 chip->can_sleep = true;
469 if (parent && parent->set_config)
470 chip->set_config = gpio_fwd_set_config;
475 chip->owner = THIS_MODULE;
476 chip->get_direction = gpio_fwd_get_direction;
477 chip->direction_input = gpio_fwd_direction_input;
478 chip->direction_output = gpio_fwd_direction_output;
479 chip->get = gpio_fwd_get;
480 chip->get_multiple = gpio_fwd_get_multiple;
481 chip->set = gpio_fwd_set;
482 chip->set_multiple = gpio_fwd_set_multiple;
484 chip->ngpio = ngpios;
488 mutex_init(&fwd->mlock);
490 spin_lock_init(&fwd->slock);
492 error = devm_gpiochip_add_data(dev, chip, fwd);
494 return ERR_PTR(error);
501 * GPIO Aggregator platform device
504 static int gpio_aggregator_probe(struct platform_device *pdev)
506 struct device *dev = &pdev->dev;
507 struct gpio_desc **descs;
508 struct gpiochip_fwd *fwd;
511 n = gpiod_count(dev, NULL);
515 descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL);
519 for (i = 0; i < n; i++) {
520 descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
521 if (IS_ERR(descs[i]))
522 return PTR_ERR(descs[i]);
525 fwd = gpiochip_fwd_create(dev, n, descs);
529 platform_set_drvdata(pdev, fwd);
534 static const struct of_device_id gpio_aggregator_dt_ids[] = {
536 * Add GPIO-operated devices controlled from userspace below,
537 * or use "driver_override" in sysfs
541 MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
544 static struct platform_driver gpio_aggregator_driver = {
545 .probe = gpio_aggregator_probe,
548 .groups = gpio_aggregator_groups,
549 .of_match_table = of_match_ptr(gpio_aggregator_dt_ids),
553 static int __init gpio_aggregator_init(void)
555 return platform_driver_register(&gpio_aggregator_driver);
557 module_init(gpio_aggregator_init);
559 static void __exit gpio_aggregator_exit(void)
561 gpio_aggregator_remove_all();
562 platform_driver_unregister(&gpio_aggregator_driver);
564 module_exit(gpio_aggregator_exit);
566 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
567 MODULE_DESCRIPTION("GPIO Aggregator");
568 MODULE_LICENSE("GPL v2");