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/delay.h>
14 #include <linux/idr.h>
15 #include <linux/kernel.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/overflow.h>
20 #include <linux/platform_device.h>
21 #include <linux/property.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/string.h>
26 #include <linux/gpio/consumer.h>
27 #include <linux/gpio/driver.h>
28 #include <linux/gpio/machine.h>
30 #define AGGREGATOR_MAX_GPIOS 512
33 * GPIO Aggregator sysfs interface
36 struct gpio_aggregator
{
37 struct gpiod_lookup_table
*lookups
;
38 struct platform_device
*pdev
;
42 static DEFINE_MUTEX(gpio_aggregator_lock
); /* protects idr */
43 static DEFINE_IDR(gpio_aggregator_idr
);
45 static int aggr_add_gpio(struct gpio_aggregator
*aggr
, const char *key
,
46 int hwnum
, unsigned int *n
)
48 struct gpiod_lookup_table
*lookups
;
50 lookups
= krealloc(aggr
->lookups
, struct_size(lookups
, table
, *n
+ 2),
55 lookups
->table
[*n
] = GPIO_LOOKUP_IDX(key
, hwnum
, NULL
, *n
, 0);
58 memset(&lookups
->table
[*n
], 0, sizeof(lookups
->table
[*n
]));
60 aggr
->lookups
= lookups
;
64 static int aggr_parse(struct gpio_aggregator
*aggr
)
66 char *args
= skip_spaces(aggr
->args
);
67 char *name
, *offsets
, *p
;
68 unsigned int i
, n
= 0;
71 unsigned long *bitmap
__free(bitmap
) =
72 bitmap_alloc(AGGREGATOR_MAX_GPIOS
, GFP_KERNEL
);
76 args
= next_arg(args
, &name
, &p
);
78 args
= next_arg(args
, &offsets
, &p
);
80 p
= get_options(offsets
, 0, &error
);
81 if (error
== 0 || *p
) {
83 error
= aggr_add_gpio(aggr
, name
, U16_MAX
, &n
);
91 /* GPIO chip + offset(s) */
92 error
= bitmap_parselist(offsets
, bitmap
, AGGREGATOR_MAX_GPIOS
);
94 pr_err("Cannot parse %s: %d\n", offsets
, error
);
98 for_each_set_bit(i
, bitmap
, AGGREGATOR_MAX_GPIOS
) {
99 error
= aggr_add_gpio(aggr
, name
, i
, &n
);
104 args
= next_arg(args
, &name
, &p
);
108 pr_err("No GPIOs specified\n");
115 static ssize_t
new_device_store(struct device_driver
*driver
, const char *buf
,
118 struct gpio_aggregator
*aggr
;
119 struct platform_device
*pdev
;
122 /* kernfs guarantees string termination, so count + 1 is safe */
123 aggr
= kzalloc(sizeof(*aggr
) + count
+ 1, GFP_KERNEL
);
127 memcpy(aggr
->args
, buf
, count
+ 1);
129 aggr
->lookups
= kzalloc(struct_size(aggr
->lookups
, table
, 1),
131 if (!aggr
->lookups
) {
136 mutex_lock(&gpio_aggregator_lock
);
137 id
= idr_alloc(&gpio_aggregator_idr
, aggr
, 0, 0, GFP_KERNEL
);
138 mutex_unlock(&gpio_aggregator_lock
);
145 aggr
->lookups
->dev_id
= kasprintf(GFP_KERNEL
, "%s.%d", DRV_NAME
, id
);
146 if (!aggr
->lookups
->dev_id
) {
151 res
= aggr_parse(aggr
);
155 gpiod_add_lookup_table(aggr
->lookups
);
157 pdev
= platform_device_register_simple(DRV_NAME
, id
, NULL
, 0);
167 gpiod_remove_lookup_table(aggr
->lookups
);
169 kfree(aggr
->lookups
->dev_id
);
171 mutex_lock(&gpio_aggregator_lock
);
172 idr_remove(&gpio_aggregator_idr
, id
);
173 mutex_unlock(&gpio_aggregator_lock
);
175 kfree(aggr
->lookups
);
181 static DRIVER_ATTR_WO(new_device
);
183 static void gpio_aggregator_free(struct gpio_aggregator
*aggr
)
185 platform_device_unregister(aggr
->pdev
);
186 gpiod_remove_lookup_table(aggr
->lookups
);
187 kfree(aggr
->lookups
->dev_id
);
188 kfree(aggr
->lookups
);
192 static ssize_t
delete_device_store(struct device_driver
*driver
,
193 const char *buf
, size_t count
)
195 struct gpio_aggregator
*aggr
;
199 if (!str_has_prefix(buf
, DRV_NAME
"."))
202 error
= kstrtouint(buf
+ strlen(DRV_NAME
"."), 10, &id
);
206 mutex_lock(&gpio_aggregator_lock
);
207 aggr
= idr_remove(&gpio_aggregator_idr
, id
);
208 mutex_unlock(&gpio_aggregator_lock
);
212 gpio_aggregator_free(aggr
);
215 static DRIVER_ATTR_WO(delete_device
);
217 static struct attribute
*gpio_aggregator_attrs
[] = {
218 &driver_attr_new_device
.attr
,
219 &driver_attr_delete_device
.attr
,
222 ATTRIBUTE_GROUPS(gpio_aggregator
);
224 static int __exit
gpio_aggregator_idr_remove(int id
, void *p
, void *data
)
226 gpio_aggregator_free(p
);
230 static void __exit
gpio_aggregator_remove_all(void)
232 mutex_lock(&gpio_aggregator_lock
);
233 idr_for_each(&gpio_aggregator_idr
, gpio_aggregator_idr_remove
, NULL
);
234 idr_destroy(&gpio_aggregator_idr
);
235 mutex_unlock(&gpio_aggregator_lock
);
243 struct gpiochip_fwd_timing
{
248 struct gpiochip_fwd
{
249 struct gpio_chip chip
;
250 struct gpio_desc
**descs
;
252 struct mutex mlock
; /* protects tmp[] if can_sleep */
253 spinlock_t slock
; /* protects tmp[] if !can_sleep */
255 struct gpiochip_fwd_timing
*delay_timings
;
256 unsigned long tmp
[]; /* values and descs for multiple ops */
259 #define fwd_tmp_values(fwd) &(fwd)->tmp[0]
260 #define fwd_tmp_descs(fwd) (void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
262 #define fwd_tmp_size(ngpios) (BITS_TO_LONGS((ngpios)) + (ngpios))
264 static int gpio_fwd_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
266 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
268 return gpiod_get_direction(fwd
->descs
[offset
]);
271 static int gpio_fwd_direction_input(struct gpio_chip
*chip
, unsigned int offset
)
273 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
275 return gpiod_direction_input(fwd
->descs
[offset
]);
278 static int gpio_fwd_direction_output(struct gpio_chip
*chip
,
279 unsigned int offset
, int value
)
281 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
283 return gpiod_direction_output(fwd
->descs
[offset
], value
);
286 static int gpio_fwd_get(struct gpio_chip
*chip
, unsigned int offset
)
288 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
290 return chip
->can_sleep
? gpiod_get_value_cansleep(fwd
->descs
[offset
])
291 : gpiod_get_value(fwd
->descs
[offset
]);
294 static int gpio_fwd_get_multiple(struct gpiochip_fwd
*fwd
, unsigned long *mask
,
297 struct gpio_desc
**descs
= fwd_tmp_descs(fwd
);
298 unsigned long *values
= fwd_tmp_values(fwd
);
299 unsigned int i
, j
= 0;
302 bitmap_clear(values
, 0, fwd
->chip
.ngpio
);
303 for_each_set_bit(i
, mask
, fwd
->chip
.ngpio
)
304 descs
[j
++] = fwd
->descs
[i
];
306 if (fwd
->chip
.can_sleep
)
307 error
= gpiod_get_array_value_cansleep(j
, descs
, NULL
, values
);
309 error
= gpiod_get_array_value(j
, descs
, NULL
, values
);
314 for_each_set_bit(i
, mask
, fwd
->chip
.ngpio
)
315 __assign_bit(i
, bits
, test_bit(j
++, values
));
320 static int gpio_fwd_get_multiple_locked(struct gpio_chip
*chip
,
321 unsigned long *mask
, unsigned long *bits
)
323 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
327 if (chip
->can_sleep
) {
328 mutex_lock(&fwd
->mlock
);
329 error
= gpio_fwd_get_multiple(fwd
, mask
, bits
);
330 mutex_unlock(&fwd
->mlock
);
332 spin_lock_irqsave(&fwd
->slock
, flags
);
333 error
= gpio_fwd_get_multiple(fwd
, mask
, bits
);
334 spin_unlock_irqrestore(&fwd
->slock
, flags
);
340 static void gpio_fwd_delay(struct gpio_chip
*chip
, unsigned int offset
, int value
)
342 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
343 const struct gpiochip_fwd_timing
*delay_timings
;
344 bool is_active_low
= gpiod_is_active_low(fwd
->descs
[offset
]);
347 delay_timings
= &fwd
->delay_timings
[offset
];
348 if ((!is_active_low
&& value
) || (is_active_low
&& !value
))
349 delay_us
= delay_timings
->ramp_up_us
;
351 delay_us
= delay_timings
->ramp_down_us
;
361 static void gpio_fwd_set(struct gpio_chip
*chip
, unsigned int offset
, int value
)
363 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
366 gpiod_set_value_cansleep(fwd
->descs
[offset
], value
);
368 gpiod_set_value(fwd
->descs
[offset
], value
);
370 if (fwd
->delay_timings
)
371 gpio_fwd_delay(chip
, offset
, value
);
374 static void gpio_fwd_set_multiple(struct gpiochip_fwd
*fwd
, unsigned long *mask
,
377 struct gpio_desc
**descs
= fwd_tmp_descs(fwd
);
378 unsigned long *values
= fwd_tmp_values(fwd
);
379 unsigned int i
, j
= 0;
381 for_each_set_bit(i
, mask
, fwd
->chip
.ngpio
) {
382 __assign_bit(j
, values
, test_bit(i
, bits
));
383 descs
[j
++] = fwd
->descs
[i
];
386 if (fwd
->chip
.can_sleep
)
387 gpiod_set_array_value_cansleep(j
, descs
, NULL
, values
);
389 gpiod_set_array_value(j
, descs
, NULL
, values
);
392 static void gpio_fwd_set_multiple_locked(struct gpio_chip
*chip
,
393 unsigned long *mask
, unsigned long *bits
)
395 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
398 if (chip
->can_sleep
) {
399 mutex_lock(&fwd
->mlock
);
400 gpio_fwd_set_multiple(fwd
, mask
, bits
);
401 mutex_unlock(&fwd
->mlock
);
403 spin_lock_irqsave(&fwd
->slock
, flags
);
404 gpio_fwd_set_multiple(fwd
, mask
, bits
);
405 spin_unlock_irqrestore(&fwd
->slock
, flags
);
409 static int gpio_fwd_set_config(struct gpio_chip
*chip
, unsigned int offset
,
410 unsigned long config
)
412 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
414 return gpiod_set_config(fwd
->descs
[offset
], config
);
417 static int gpio_fwd_to_irq(struct gpio_chip
*chip
, unsigned int offset
)
419 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
421 return gpiod_to_irq(fwd
->descs
[offset
]);
425 * The GPIO delay provides a way to configure platform specific delays
426 * for the GPIO ramp-up or ramp-down delays. This can serve the following
428 * - Open-drain output using an RC filter
430 #define FWD_FEATURE_DELAY BIT(0)
432 #ifdef CONFIG_OF_GPIO
433 static int gpiochip_fwd_delay_of_xlate(struct gpio_chip
*chip
,
434 const struct of_phandle_args
*gpiospec
,
437 struct gpiochip_fwd
*fwd
= gpiochip_get_data(chip
);
438 struct gpiochip_fwd_timing
*timings
;
441 if (gpiospec
->args_count
!= chip
->of_gpio_n_cells
)
444 line
= gpiospec
->args
[0];
445 if (line
>= chip
->ngpio
)
448 timings
= &fwd
->delay_timings
[line
];
449 timings
->ramp_up_us
= gpiospec
->args
[1];
450 timings
->ramp_down_us
= gpiospec
->args
[2];
455 static int gpiochip_fwd_setup_delay_line(struct device
*dev
, struct gpio_chip
*chip
,
456 struct gpiochip_fwd
*fwd
)
458 fwd
->delay_timings
= devm_kcalloc(dev
, chip
->ngpio
,
459 sizeof(*fwd
->delay_timings
),
461 if (!fwd
->delay_timings
)
464 chip
->of_xlate
= gpiochip_fwd_delay_of_xlate
;
465 chip
->of_gpio_n_cells
= 3;
470 static int gpiochip_fwd_setup_delay_line(struct device
*dev
, struct gpio_chip
*chip
,
471 struct gpiochip_fwd
*fwd
)
475 #endif /* !CONFIG_OF_GPIO */
478 * gpiochip_fwd_create() - Create a new GPIO forwarder
479 * @dev: Parent device pointer
480 * @ngpios: Number of GPIOs in the forwarder.
481 * @descs: Array containing the GPIO descriptors to forward to.
482 * This array must contain @ngpios entries, and must not be deallocated
483 * before the forwarder has been destroyed again.
484 * @features: Bitwise ORed features as defined with FWD_FEATURE_*.
486 * This function creates a new gpiochip, which forwards all GPIO operations to
487 * the passed GPIO descriptors.
489 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
492 static struct gpiochip_fwd
*gpiochip_fwd_create(struct device
*dev
,
494 struct gpio_desc
*descs
[],
495 unsigned long features
)
497 const char *label
= dev_name(dev
);
498 struct gpiochip_fwd
*fwd
;
499 struct gpio_chip
*chip
;
503 fwd
= devm_kzalloc(dev
, struct_size(fwd
, tmp
, fwd_tmp_size(ngpios
)),
506 return ERR_PTR(-ENOMEM
);
511 * If any of the GPIO lines are sleeping, then the entire forwarder
513 * If any of the chips support .set_config(), then the forwarder will
514 * support setting configs.
516 for (i
= 0; i
< ngpios
; i
++) {
517 struct gpio_chip
*parent
= gpiod_to_chip(descs
[i
]);
519 dev_dbg(dev
, "%u => gpio %d irq %d\n", i
,
520 desc_to_gpio(descs
[i
]), gpiod_to_irq(descs
[i
]));
522 if (gpiod_cansleep(descs
[i
]))
523 chip
->can_sleep
= true;
524 if (parent
&& parent
->set_config
)
525 chip
->set_config
= gpio_fwd_set_config
;
530 chip
->owner
= THIS_MODULE
;
531 chip
->get_direction
= gpio_fwd_get_direction
;
532 chip
->direction_input
= gpio_fwd_direction_input
;
533 chip
->direction_output
= gpio_fwd_direction_output
;
534 chip
->get
= gpio_fwd_get
;
535 chip
->get_multiple
= gpio_fwd_get_multiple_locked
;
536 chip
->set
= gpio_fwd_set
;
537 chip
->set_multiple
= gpio_fwd_set_multiple_locked
;
538 chip
->to_irq
= gpio_fwd_to_irq
;
540 chip
->ngpio
= ngpios
;
544 mutex_init(&fwd
->mlock
);
546 spin_lock_init(&fwd
->slock
);
548 if (features
& FWD_FEATURE_DELAY
) {
549 error
= gpiochip_fwd_setup_delay_line(dev
, chip
, fwd
);
551 return ERR_PTR(error
);
554 error
= devm_gpiochip_add_data(dev
, chip
, fwd
);
556 return ERR_PTR(error
);
563 * GPIO Aggregator platform device
566 static int gpio_aggregator_probe(struct platform_device
*pdev
)
568 struct device
*dev
= &pdev
->dev
;
569 struct gpio_desc
**descs
;
570 struct gpiochip_fwd
*fwd
;
571 unsigned long features
;
574 n
= gpiod_count(dev
, NULL
);
578 descs
= devm_kmalloc_array(dev
, n
, sizeof(*descs
), GFP_KERNEL
);
582 for (i
= 0; i
< n
; i
++) {
583 descs
[i
] = devm_gpiod_get_index(dev
, NULL
, i
, GPIOD_ASIS
);
584 if (IS_ERR(descs
[i
]))
585 return PTR_ERR(descs
[i
]);
588 features
= (uintptr_t)device_get_match_data(dev
);
589 fwd
= gpiochip_fwd_create(dev
, n
, descs
, features
);
593 platform_set_drvdata(pdev
, fwd
);
597 static const struct of_device_id gpio_aggregator_dt_ids
[] = {
599 .compatible
= "gpio-delay",
600 .data
= (void *)FWD_FEATURE_DELAY
,
603 * Add GPIO-operated devices controlled from userspace below,
604 * or use "driver_override" in sysfs.
608 MODULE_DEVICE_TABLE(of
, gpio_aggregator_dt_ids
);
610 static struct platform_driver gpio_aggregator_driver
= {
611 .probe
= gpio_aggregator_probe
,
614 .groups
= gpio_aggregator_groups
,
615 .of_match_table
= gpio_aggregator_dt_ids
,
619 static int __init
gpio_aggregator_init(void)
621 return platform_driver_register(&gpio_aggregator_driver
);
623 module_init(gpio_aggregator_init
);
625 static void __exit
gpio_aggregator_exit(void)
627 gpio_aggregator_remove_all();
628 platform_driver_unregister(&gpio_aggregator_driver
);
630 module_exit(gpio_aggregator_exit
);
632 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
633 MODULE_DESCRIPTION("GPIO Aggregator");
634 MODULE_LICENSE("GPL v2");