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/idr.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/overflow.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/string.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/gpio/driver.h>
25 #include <linux/gpio/machine.h>
27 #define AGGREGATOR_MAX_GPIOS 512
30 * GPIO Aggregator sysfs interface
33 struct gpio_aggregator {
34 struct gpiod_lookup_table *lookups;
35 struct platform_device *pdev;
39 static DEFINE_MUTEX(gpio_aggregator_lock); /* protects idr */
40 static DEFINE_IDR(gpio_aggregator_idr);
42 static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,
43 int hwnum, unsigned int *n)
45 struct gpiod_lookup_table *lookups;
47 lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2),
52 lookups->table[*n] = GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0);
55 memset(&lookups->table[*n], 0, sizeof(lookups->table[*n]));
57 aggr->lookups = lookups;
61 static int aggr_parse(struct gpio_aggregator *aggr)
63 char *args = skip_spaces(aggr->args);
64 char *name, *offsets, *p;
65 unsigned long *bitmap;
66 unsigned int i, n = 0;
69 bitmap = bitmap_alloc(AGGREGATOR_MAX_GPIOS, GFP_KERNEL);
73 args = next_arg(args, &name, &p);
75 args = next_arg(args, &offsets, &p);
77 p = get_options(offsets, 0, &error);
78 if (error == 0 || *p) {
80 error = aggr_add_gpio(aggr, name, U16_MAX, &n);
88 /* GPIO chip + offset(s) */
89 error = bitmap_parselist(offsets, bitmap, AGGREGATOR_MAX_GPIOS);
91 pr_err("Cannot parse %s: %d\n", offsets, error);
95 for_each_set_bit(i, bitmap, AGGREGATOR_MAX_GPIOS) {
96 error = aggr_add_gpio(aggr, name, i, &n);
101 args = next_arg(args, &name, &p);
105 pr_err("No GPIOs specified\n");
114 static ssize_t new_device_store(struct device_driver *driver, const char *buf,
117 struct gpio_aggregator *aggr;
118 struct platform_device *pdev;
121 /* kernfs guarantees string termination, so count + 1 is safe */
122 aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL);
126 memcpy(aggr->args, buf, count + 1);
128 aggr->lookups = kzalloc(struct_size(aggr->lookups, table, 1),
130 if (!aggr->lookups) {
135 mutex_lock(&gpio_aggregator_lock);
136 id = idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL);
137 mutex_unlock(&gpio_aggregator_lock);
144 aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id);
145 if (!aggr->lookups->dev_id) {
150 res = aggr_parse(aggr);
154 gpiod_add_lookup_table(aggr->lookups);
156 pdev = platform_device_register_simple(DRV_NAME, id, NULL, 0);
166 gpiod_remove_lookup_table(aggr->lookups);
168 kfree(aggr->lookups->dev_id);
170 mutex_lock(&gpio_aggregator_lock);
171 idr_remove(&gpio_aggregator_idr, id);
172 mutex_unlock(&gpio_aggregator_lock);
174 kfree(aggr->lookups);
180 static DRIVER_ATTR_WO(new_device);
182 static void gpio_aggregator_free(struct gpio_aggregator *aggr)
184 platform_device_unregister(aggr->pdev);
185 gpiod_remove_lookup_table(aggr->lookups);
186 kfree(aggr->lookups->dev_id);
187 kfree(aggr->lookups);
191 static ssize_t delete_device_store(struct device_driver *driver,
192 const char *buf, size_t count)
194 struct gpio_aggregator *aggr;
198 if (!str_has_prefix(buf, DRV_NAME "."))
201 error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id);
205 mutex_lock(&gpio_aggregator_lock);
206 aggr = idr_remove(&gpio_aggregator_idr, id);
207 mutex_unlock(&gpio_aggregator_lock);
211 gpio_aggregator_free(aggr);
214 static DRIVER_ATTR_WO(delete_device);
216 static struct attribute *gpio_aggregator_attrs[] = {
217 &driver_attr_new_device.attr,
218 &driver_attr_delete_device.attr,
221 ATTRIBUTE_GROUPS(gpio_aggregator);
223 static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data)
225 gpio_aggregator_free(p);
229 static void __exit gpio_aggregator_remove_all(void)
231 mutex_lock(&gpio_aggregator_lock);
232 idr_for_each(&gpio_aggregator_idr, gpio_aggregator_idr_remove, NULL);
233 idr_destroy(&gpio_aggregator_idr);
234 mutex_unlock(&gpio_aggregator_lock);
242 struct gpiochip_fwd {
243 struct gpio_chip chip;
244 struct gpio_desc **descs;
246 struct mutex mlock; /* protects tmp[] if can_sleep */
247 spinlock_t slock; /* protects tmp[] if !can_sleep */
249 unsigned long tmp[]; /* values and descs for multiple ops */
252 #define fwd_tmp_values(fwd) &(fwd)->tmp[0]
253 #define fwd_tmp_descs(fwd) (void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
255 #define fwd_tmp_size(ngpios) (BITS_TO_LONGS((ngpios)) + (ngpios))
257 static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
259 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
261 return gpiod_get_direction(fwd->descs[offset]);
264 static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset)
266 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
268 return gpiod_direction_input(fwd->descs[offset]);
271 static int gpio_fwd_direction_output(struct gpio_chip *chip,
272 unsigned int offset, int value)
274 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
276 return gpiod_direction_output(fwd->descs[offset], value);
279 static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
281 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
283 return chip->can_sleep ? gpiod_get_value_cansleep(fwd->descs[offset])
284 : gpiod_get_value(fwd->descs[offset]);
287 static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
290 struct gpio_desc **descs = fwd_tmp_descs(fwd);
291 unsigned long *values = fwd_tmp_values(fwd);
292 unsigned int i, j = 0;
295 bitmap_clear(values, 0, fwd->chip.ngpio);
296 for_each_set_bit(i, mask, fwd->chip.ngpio)
297 descs[j++] = fwd->descs[i];
299 if (fwd->chip.can_sleep)
300 error = gpiod_get_array_value_cansleep(j, descs, NULL, values);
302 error = gpiod_get_array_value(j, descs, NULL, values);
307 for_each_set_bit(i, mask, fwd->chip.ngpio)
308 __assign_bit(i, bits, test_bit(j++, values));
313 static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
314 unsigned long *mask, unsigned long *bits)
316 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
320 if (chip->can_sleep) {
321 mutex_lock(&fwd->mlock);
322 error = gpio_fwd_get_multiple(fwd, mask, bits);
323 mutex_unlock(&fwd->mlock);
325 spin_lock_irqsave(&fwd->slock, flags);
326 error = gpio_fwd_get_multiple(fwd, mask, bits);
327 spin_unlock_irqrestore(&fwd->slock, flags);
333 static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
335 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
338 gpiod_set_value_cansleep(fwd->descs[offset], value);
340 gpiod_set_value(fwd->descs[offset], value);
343 static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
346 struct gpio_desc **descs = fwd_tmp_descs(fwd);
347 unsigned long *values = fwd_tmp_values(fwd);
348 unsigned int i, j = 0;
350 for_each_set_bit(i, mask, fwd->chip.ngpio) {
351 __assign_bit(j, values, test_bit(i, bits));
352 descs[j++] = fwd->descs[i];
355 if (fwd->chip.can_sleep)
356 gpiod_set_array_value_cansleep(j, descs, NULL, values);
358 gpiod_set_array_value(j, descs, NULL, values);
361 static void gpio_fwd_set_multiple_locked(struct gpio_chip *chip,
362 unsigned long *mask, unsigned long *bits)
364 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
367 if (chip->can_sleep) {
368 mutex_lock(&fwd->mlock);
369 gpio_fwd_set_multiple(fwd, mask, bits);
370 mutex_unlock(&fwd->mlock);
372 spin_lock_irqsave(&fwd->slock, flags);
373 gpio_fwd_set_multiple(fwd, mask, bits);
374 spin_unlock_irqrestore(&fwd->slock, flags);
378 static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
379 unsigned long config)
381 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
383 return gpiod_set_config(fwd->descs[offset], config);
386 static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset)
388 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
390 return gpiod_to_irq(fwd->descs[offset]);
394 * gpiochip_fwd_create() - Create a new GPIO forwarder
395 * @dev: Parent device pointer
396 * @ngpios: Number of GPIOs in the forwarder.
397 * @descs: Array containing the GPIO descriptors to forward to.
398 * This array must contain @ngpios entries, and must not be deallocated
399 * before the forwarder has been destroyed again.
401 * This function creates a new gpiochip, which forwards all GPIO operations to
402 * the passed GPIO descriptors.
404 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
407 static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
409 struct gpio_desc *descs[])
411 const char *label = dev_name(dev);
412 struct gpiochip_fwd *fwd;
413 struct gpio_chip *chip;
417 fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
420 return ERR_PTR(-ENOMEM);
425 * If any of the GPIO lines are sleeping, then the entire forwarder
427 * If any of the chips support .set_config(), then the forwarder will
428 * support setting configs.
430 for (i = 0; i < ngpios; i++) {
431 struct gpio_chip *parent = gpiod_to_chip(descs[i]);
433 dev_dbg(dev, "%u => gpio %d irq %d\n", i,
434 desc_to_gpio(descs[i]), gpiod_to_irq(descs[i]));
436 if (gpiod_cansleep(descs[i]))
437 chip->can_sleep = true;
438 if (parent && parent->set_config)
439 chip->set_config = gpio_fwd_set_config;
444 chip->owner = THIS_MODULE;
445 chip->get_direction = gpio_fwd_get_direction;
446 chip->direction_input = gpio_fwd_direction_input;
447 chip->direction_output = gpio_fwd_direction_output;
448 chip->get = gpio_fwd_get;
449 chip->get_multiple = gpio_fwd_get_multiple_locked;
450 chip->set = gpio_fwd_set;
451 chip->set_multiple = gpio_fwd_set_multiple_locked;
452 chip->to_irq = gpio_fwd_to_irq;
454 chip->ngpio = ngpios;
458 mutex_init(&fwd->mlock);
460 spin_lock_init(&fwd->slock);
462 error = devm_gpiochip_add_data(dev, chip, fwd);
464 return ERR_PTR(error);
471 * GPIO Aggregator platform device
474 static int gpio_aggregator_probe(struct platform_device *pdev)
476 struct device *dev = &pdev->dev;
477 struct gpio_desc **descs;
478 struct gpiochip_fwd *fwd;
481 n = gpiod_count(dev, NULL);
485 descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL);
489 for (i = 0; i < n; i++) {
490 descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
491 if (IS_ERR(descs[i]))
492 return PTR_ERR(descs[i]);
495 fwd = gpiochip_fwd_create(dev, n, descs);
499 platform_set_drvdata(pdev, fwd);
504 static const struct of_device_id gpio_aggregator_dt_ids[] = {
506 * Add GPIO-operated devices controlled from userspace below,
507 * or use "driver_override" in sysfs
511 MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
514 static struct platform_driver gpio_aggregator_driver = {
515 .probe = gpio_aggregator_probe,
518 .groups = gpio_aggregator_groups,
519 .of_match_table = of_match_ptr(gpio_aggregator_dt_ids),
523 static int __init gpio_aggregator_init(void)
525 return platform_driver_register(&gpio_aggregator_driver);
527 module_init(gpio_aggregator_init);
529 static void __exit gpio_aggregator_exit(void)
531 gpio_aggregator_remove_all();
532 platform_driver_unregister(&gpio_aggregator_driver);
534 module_exit(gpio_aggregator_exit);
536 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
537 MODULE_DESCRIPTION("GPIO Aggregator");
538 MODULE_LICENSE("GPL v2");