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 char *get_arg(char **args
)
44 start
= skip_spaces(*args
);
50 end
= strchr(++start
, '"');
52 return ERR_PTR(-EINVAL
);
55 for (end
= start
; *end
&& !isspace(*end
); end
++) ;
65 static bool isrange(const char *s
)
69 if (IS_ERR_OR_NULL(s
))
73 n
= strspn(s
, "0123456789");
93 static int aggr_add_gpio(struct gpio_aggregator
*aggr
, const char *key
,
94 int hwnum
, unsigned int *n
)
96 struct gpiod_lookup_table
*lookups
;
98 lookups
= krealloc(aggr
->lookups
, struct_size(lookups
, table
, *n
+ 2),
104 (struct gpiod_lookup
)GPIO_LOOKUP_IDX(key
, hwnum
, NULL
, *n
, 0);
107 memset(&lookups
->table
[*n
], 0, sizeof(lookups
->table
[*n
]));
109 aggr
->lookups
= lookups
;
113 static int aggr_parse(struct gpio_aggregator
*aggr
)
115 char *args
= aggr
->args
;
116 unsigned long *bitmap
;
117 unsigned int i
, n
= 0;
118 char *name
, *offsets
;
121 bitmap
= bitmap_alloc(ARCH_NR_GPIOS
, GFP_KERNEL
);
125 for (name
= get_arg(&args
), offsets
= get_arg(&args
); name
;
126 offsets
= get_arg(&args
)) {
128 pr_err("Cannot get GPIO specifier: %pe\n", name
);
129 error
= PTR_ERR(name
);
133 if (!isrange(offsets
)) {
134 /* Named GPIO line */
135 error
= aggr_add_gpio(aggr
, name
, U16_MAX
, &n
);
143 /* GPIO chip + offset(s) */
144 error
= bitmap_parselist(offsets
, bitmap
, ARCH_NR_GPIOS
);
146 pr_err("Cannot parse %s: %d\n", offsets
, error
);
150 for_each_set_bit(i
, bitmap
, ARCH_NR_GPIOS
) {
151 error
= aggr_add_gpio(aggr
, name
, i
, &n
);
156 name
= get_arg(&args
);
160 pr_err("No GPIOs specified\n");
169 static ssize_t
new_device_store(struct device_driver
*driver
, const char *buf
,
172 struct gpio_aggregator
*aggr
;
173 struct platform_device
*pdev
;
176 /* kernfs guarantees string termination, so count + 1 is safe */
177 aggr
= kzalloc(sizeof(*aggr
) + count
+ 1, GFP_KERNEL
);
181 memcpy(aggr
->args
, buf
, count
+ 1);
183 aggr
->lookups
= kzalloc(struct_size(aggr
->lookups
, table
, 1),
185 if (!aggr
->lookups
) {
190 mutex_lock(&gpio_aggregator_lock
);
191 id
= idr_alloc(&gpio_aggregator_idr
, aggr
, 0, 0, GFP_KERNEL
);
192 mutex_unlock(&gpio_aggregator_lock
);
199 aggr
->lookups
->dev_id
= kasprintf(GFP_KERNEL
, "%s.%d", DRV_NAME
, id
);
200 if (!aggr
->lookups
->dev_id
) {
205 res
= aggr_parse(aggr
);
209 gpiod_add_lookup_table(aggr
->lookups
);
211 pdev
= platform_device_register_simple(DRV_NAME
, id
, NULL
, 0);
221 gpiod_remove_lookup_table(aggr
->lookups
);
223 kfree(aggr
->lookups
->dev_id
);
225 mutex_lock(&gpio_aggregator_lock
);
226 idr_remove(&gpio_aggregator_idr
, id
);
227 mutex_unlock(&gpio_aggregator_lock
);
229 kfree(aggr
->lookups
);
235 static DRIVER_ATTR_WO(new_device
);
237 static void gpio_aggregator_free(struct gpio_aggregator
*aggr
)
239 platform_device_unregister(aggr
->pdev
);
240 gpiod_remove_lookup_table(aggr
->lookups
);
241 kfree(aggr
->lookups
->dev_id
);
242 kfree(aggr
->lookups
);
246 static ssize_t
delete_device_store(struct device_driver
*driver
,
247 const char *buf
, size_t count
)
249 struct gpio_aggregator
*aggr
;
253 if (!str_has_prefix(buf
, DRV_NAME
"."))
256 error
= kstrtouint(buf
+ strlen(DRV_NAME
"."), 10, &id
);
260 mutex_lock(&gpio_aggregator_lock
);
261 aggr
= idr_remove(&gpio_aggregator_idr
, id
);
262 mutex_unlock(&gpio_aggregator_lock
);
266 gpio_aggregator_free(aggr
);
269 static DRIVER_ATTR_WO(delete_device
);
271 static struct attribute
*gpio_aggregator_attrs
[] = {
272 &driver_attr_new_device
.attr
,
273 &driver_attr_delete_device
.attr
,
276 ATTRIBUTE_GROUPS(gpio_aggregator
);
278 static int __exit
gpio_aggregator_idr_remove(int id
, void *p
, void *data
)
280 gpio_aggregator_free(p
);
284 static void __exit
gpio_aggregator_remove_all(void)
286 mutex_lock(&gpio_aggregator_lock
);
287 idr_for_each(&gpio_aggregator_idr
, gpio_aggregator_idr_remove
, NULL
);
288 idr_destroy(&gpio_aggregator_idr
);
289 mutex_unlock(&gpio_aggregator_lock
);
297 struct gpiochip_fwd
{
298 struct gpio_chip chip
;
299 struct gpio_desc
**descs
;
301 struct mutex mlock
; /* protects tmp[] if can_sleep */
302 spinlock_t slock
; /* protects tmp[] if !can_sleep */
304 unsigned long tmp
[]; /* values and descs for multiple ops */
307 static int gpio_fwd_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
309 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
311 return gpiod_get_direction(fwd
->descs
[offset
]);
314 static int gpio_fwd_direction_input(struct gpio_chip
*chip
, unsigned int offset
)
316 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
318 return gpiod_direction_input(fwd
->descs
[offset
]);
321 static int gpio_fwd_direction_output(struct gpio_chip
*chip
,
322 unsigned int offset
, int value
)
324 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
326 return gpiod_direction_output(fwd
->descs
[offset
], value
);
329 static int gpio_fwd_get(struct gpio_chip
*chip
, unsigned int offset
)
331 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
333 return gpiod_get_value(fwd
->descs
[offset
]);
336 static int gpio_fwd_get_multiple(struct gpiochip_fwd
*fwd
, unsigned long *mask
,
339 struct gpio_desc
**descs
;
340 unsigned long *values
;
341 unsigned int i
, j
= 0;
344 /* Both values bitmap and desc pointers are stored in tmp[] */
345 values
= &fwd
->tmp
[0];
346 descs
= (void *)&fwd
->tmp
[BITS_TO_LONGS(fwd
->chip
.ngpio
)];
348 bitmap_clear(values
, 0, fwd
->chip
.ngpio
);
349 for_each_set_bit(i
, mask
, fwd
->chip
.ngpio
)
350 descs
[j
++] = fwd
->descs
[i
];
352 error
= gpiod_get_array_value(j
, descs
, NULL
, values
);
357 for_each_set_bit(i
, mask
, fwd
->chip
.ngpio
)
358 __assign_bit(i
, bits
, test_bit(j
++, values
));
363 static int gpio_fwd_get_multiple_locked(struct gpio_chip
*chip
,
364 unsigned long *mask
, unsigned long *bits
)
366 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
370 if (chip
->can_sleep
) {
371 mutex_lock(&fwd
->mlock
);
372 error
= gpio_fwd_get_multiple(fwd
, mask
, bits
);
373 mutex_unlock(&fwd
->mlock
);
375 spin_lock_irqsave(&fwd
->slock
, flags
);
376 error
= gpio_fwd_get_multiple(fwd
, mask
, bits
);
377 spin_unlock_irqrestore(&fwd
->slock
, flags
);
383 static void gpio_fwd_set(struct gpio_chip
*chip
, unsigned int offset
, int value
)
385 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
387 gpiod_set_value(fwd
->descs
[offset
], value
);
390 static void gpio_fwd_set_multiple(struct gpiochip_fwd
*fwd
, unsigned long *mask
,
393 struct gpio_desc
**descs
;
394 unsigned long *values
;
395 unsigned int i
, j
= 0;
397 /* Both values bitmap and desc pointers are stored in tmp[] */
398 values
= &fwd
->tmp
[0];
399 descs
= (void *)&fwd
->tmp
[BITS_TO_LONGS(fwd
->chip
.ngpio
)];
401 for_each_set_bit(i
, mask
, fwd
->chip
.ngpio
) {
402 __assign_bit(j
, values
, test_bit(i
, bits
));
403 descs
[j
++] = fwd
->descs
[i
];
406 gpiod_set_array_value(j
, descs
, NULL
, values
);
409 static void gpio_fwd_set_multiple_locked(struct gpio_chip
*chip
,
410 unsigned long *mask
, unsigned long *bits
)
412 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
415 if (chip
->can_sleep
) {
416 mutex_lock(&fwd
->mlock
);
417 gpio_fwd_set_multiple(fwd
, mask
, bits
);
418 mutex_unlock(&fwd
->mlock
);
420 spin_lock_irqsave(&fwd
->slock
, flags
);
421 gpio_fwd_set_multiple(fwd
, mask
, bits
);
422 spin_unlock_irqrestore(&fwd
->slock
, flags
);
426 static int gpio_fwd_set_config(struct gpio_chip
*chip
, unsigned int offset
,
427 unsigned long config
)
429 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
431 return gpiod_set_config(fwd
->descs
[offset
], config
);
435 * gpiochip_fwd_create() - Create a new GPIO forwarder
436 * @dev: Parent device pointer
437 * @ngpios: Number of GPIOs in the forwarder.
438 * @descs: Array containing the GPIO descriptors to forward to.
439 * This array must contain @ngpios entries, and must not be deallocated
440 * before the forwarder has been destroyed again.
442 * This function creates a new gpiochip, which forwards all GPIO operations to
443 * the passed GPIO descriptors.
445 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
448 static struct gpiochip_fwd
*gpiochip_fwd_create(struct device
*dev
,
450 struct gpio_desc
*descs
[])
452 const char *label
= dev_name(dev
);
453 struct gpiochip_fwd
*fwd
;
454 struct gpio_chip
*chip
;
458 fwd
= devm_kzalloc(dev
, struct_size(fwd
, tmp
,
459 BITS_TO_LONGS(ngpios
) + ngpios
), GFP_KERNEL
);
461 return ERR_PTR(-ENOMEM
);
466 * If any of the GPIO lines are sleeping, then the entire forwarder
468 * If any of the chips support .set_config(), then the forwarder will
469 * support setting configs.
471 for (i
= 0; i
< ngpios
; i
++) {
472 struct gpio_chip
*parent
= gpiod_to_chip(descs
[i
]);
474 dev_dbg(dev
, "%u => gpio-%d\n", i
, desc_to_gpio(descs
[i
]));
476 if (gpiod_cansleep(descs
[i
]))
477 chip
->can_sleep
= true;
478 if (parent
&& parent
->set_config
)
479 chip
->set_config
= gpio_fwd_set_config
;
484 chip
->owner
= THIS_MODULE
;
485 chip
->get_direction
= gpio_fwd_get_direction
;
486 chip
->direction_input
= gpio_fwd_direction_input
;
487 chip
->direction_output
= gpio_fwd_direction_output
;
488 chip
->get
= gpio_fwd_get
;
489 chip
->get_multiple
= gpio_fwd_get_multiple_locked
;
490 chip
->set
= gpio_fwd_set
;
491 chip
->set_multiple
= gpio_fwd_set_multiple_locked
;
493 chip
->ngpio
= ngpios
;
497 mutex_init(&fwd
->mlock
);
499 spin_lock_init(&fwd
->slock
);
501 error
= devm_gpiochip_add_data(dev
, chip
, fwd
);
503 return ERR_PTR(error
);
510 * GPIO Aggregator platform device
513 static int gpio_aggregator_probe(struct platform_device
*pdev
)
515 struct device
*dev
= &pdev
->dev
;
516 struct gpio_desc
**descs
;
517 struct gpiochip_fwd
*fwd
;
520 n
= gpiod_count(dev
, NULL
);
524 descs
= devm_kmalloc_array(dev
, n
, sizeof(*descs
), GFP_KERNEL
);
528 for (i
= 0; i
< n
; i
++) {
529 descs
[i
] = devm_gpiod_get_index(dev
, NULL
, i
, GPIOD_ASIS
);
530 if (IS_ERR(descs
[i
]))
531 return PTR_ERR(descs
[i
]);
534 fwd
= gpiochip_fwd_create(dev
, n
, descs
);
538 platform_set_drvdata(pdev
, fwd
);
543 static const struct of_device_id gpio_aggregator_dt_ids
[] = {
545 * Add GPIO-operated devices controlled from userspace below,
546 * or use "driver_override" in sysfs
550 MODULE_DEVICE_TABLE(of
, gpio_aggregator_dt_ids
);
553 static struct platform_driver gpio_aggregator_driver
= {
554 .probe
= gpio_aggregator_probe
,
557 .groups
= gpio_aggregator_groups
,
558 .of_match_table
= of_match_ptr(gpio_aggregator_dt_ids
),
562 static int __init
gpio_aggregator_init(void)
564 return platform_driver_register(&gpio_aggregator_driver
);
566 module_init(gpio_aggregator_init
);
568 static void __exit
gpio_aggregator_exit(void)
570 gpio_aggregator_remove_all();
571 platform_driver_unregister(&gpio_aggregator_driver
);
573 module_exit(gpio_aggregator_exit
);
575 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
576 MODULE_DESCRIPTION("GPIO Aggregator");
577 MODULE_LICENSE("GPL v2");