1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // GPIO Aggregator
4 //
5 // Copyright (C) 2019-2020 Glider bv
6
7 #define DRV_NAME "gpio-aggregator"
8 #define pr_fmt(fmt) DRV_NAME ": " fmt
9
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>
25
26 #include <linux/gpio/consumer.h>
27 #include <linux/gpio/driver.h>
28 #include <linux/gpio/machine.h>
29
30 #define AGGREGATOR_MAX_GPIOS 512
31
32 /*
33 * GPIO Aggregator sysfs interface
34 */
35
36 struct gpio_aggregator {
37 struct gpiod_lookup_table *lookups;
38 struct platform_device *pdev;
39 char args[];
40 };
41
42 static DEFINE_MUTEX(gpio_aggregator_lock); /* protects idr */
43 static DEFINE_IDR(gpio_aggregator_idr);
44
aggr_add_gpio(struct gpio_aggregator * aggr,const char * key,int hwnum,unsigned int * n)45 static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,
46 int hwnum, unsigned int *n)
47 {
48 struct gpiod_lookup_table *lookups;
49
50 lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2),
51 GFP_KERNEL);
52 if (!lookups)
53 return -ENOMEM;
54
55 lookups->table[*n] = GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0);
56
57 (*n)++;
58 memset(&lookups->table[*n], 0, sizeof(lookups->table[*n]));
59
60 aggr->lookups = lookups;
61 return 0;
62 }
63
aggr_parse(struct gpio_aggregator * aggr)64 static int aggr_parse(struct gpio_aggregator *aggr)
65 {
66 char *args = skip_spaces(aggr->args);
67 char *name, *offsets, *p;
68 unsigned int i, n = 0;
69 int error = 0;
70
71 unsigned long *bitmap __free(bitmap) =
72 bitmap_alloc(AGGREGATOR_MAX_GPIOS, GFP_KERNEL);
73 if (!bitmap)
74 return -ENOMEM;
75
76 args = next_arg(args, &name, &p);
77 while (*args) {
78 args = next_arg(args, &offsets, &p);
79
80 p = get_options(offsets, 0, &error);
81 if (error == 0 || *p) {
82 /* Named GPIO line */
83 error = aggr_add_gpio(aggr, name, U16_MAX, &n);
84 if (error)
85 return error;
86
87 name = offsets;
88 continue;
89 }
90
91 /* GPIO chip + offset(s) */
92 error = bitmap_parselist(offsets, bitmap, AGGREGATOR_MAX_GPIOS);
93 if (error) {
94 pr_err("Cannot parse %s: %d\n", offsets, error);
95 return error;
96 }
97
98 for_each_set_bit(i, bitmap, AGGREGATOR_MAX_GPIOS) {
99 error = aggr_add_gpio(aggr, name, i, &n);
100 if (error)
101 return error;
102 }
103
104 args = next_arg(args, &name, &p);
105 }
106
107 if (!n) {
108 pr_err("No GPIOs specified\n");
109 return -EINVAL;
110 }
111
112 return 0;
113 }
114
new_device_store(struct device_driver * driver,const char * buf,size_t count)115 static ssize_t new_device_store(struct device_driver *driver, const char *buf,
116 size_t count)
117 {
118 struct gpio_aggregator *aggr;
119 struct platform_device *pdev;
120 int res, id;
121
122 if (!try_module_get(THIS_MODULE))
123 return -ENOENT;
124
125 /* kernfs guarantees string termination, so count + 1 is safe */
126 aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL);
127 if (!aggr) {
128 res = -ENOMEM;
129 goto put_module;
130 }
131
132 memcpy(aggr->args, buf, count + 1);
133
134 aggr->lookups = kzalloc(struct_size(aggr->lookups, table, 1),
135 GFP_KERNEL);
136 if (!aggr->lookups) {
137 res = -ENOMEM;
138 goto free_ga;
139 }
140
141 mutex_lock(&gpio_aggregator_lock);
142 id = idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL);
143 mutex_unlock(&gpio_aggregator_lock);
144
145 if (id < 0) {
146 res = id;
147 goto free_table;
148 }
149
150 aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id);
151 if (!aggr->lookups->dev_id) {
152 res = -ENOMEM;
153 goto remove_idr;
154 }
155
156 res = aggr_parse(aggr);
157 if (res)
158 goto free_dev_id;
159
160 gpiod_add_lookup_table(aggr->lookups);
161
162 pdev = platform_device_register_simple(DRV_NAME, id, NULL, 0);
163 if (IS_ERR(pdev)) {
164 res = PTR_ERR(pdev);
165 goto remove_table;
166 }
167
168 aggr->pdev = pdev;
169 module_put(THIS_MODULE);
170 return count;
171
172 remove_table:
173 gpiod_remove_lookup_table(aggr->lookups);
174 free_dev_id:
175 kfree(aggr->lookups->dev_id);
176 remove_idr:
177 mutex_lock(&gpio_aggregator_lock);
178 idr_remove(&gpio_aggregator_idr, id);
179 mutex_unlock(&gpio_aggregator_lock);
180 free_table:
181 kfree(aggr->lookups);
182 free_ga:
183 kfree(aggr);
184 put_module:
185 module_put(THIS_MODULE);
186 return res;
187 }
188
189 static DRIVER_ATTR_WO(new_device);
190
gpio_aggregator_free(struct gpio_aggregator * aggr)191 static void gpio_aggregator_free(struct gpio_aggregator *aggr)
192 {
193 platform_device_unregister(aggr->pdev);
194 gpiod_remove_lookup_table(aggr->lookups);
195 kfree(aggr->lookups->dev_id);
196 kfree(aggr->lookups);
197 kfree(aggr);
198 }
199
delete_device_store(struct device_driver * driver,const char * buf,size_t count)200 static ssize_t delete_device_store(struct device_driver *driver,
201 const char *buf, size_t count)
202 {
203 struct gpio_aggregator *aggr;
204 unsigned int id;
205 int error;
206
207 if (!str_has_prefix(buf, DRV_NAME "."))
208 return -EINVAL;
209
210 error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id);
211 if (error)
212 return error;
213
214 if (!try_module_get(THIS_MODULE))
215 return -ENOENT;
216
217 mutex_lock(&gpio_aggregator_lock);
218 aggr = idr_remove(&gpio_aggregator_idr, id);
219 mutex_unlock(&gpio_aggregator_lock);
220 if (!aggr) {
221 module_put(THIS_MODULE);
222 return -ENOENT;
223 }
224
225 gpio_aggregator_free(aggr);
226 module_put(THIS_MODULE);
227 return count;
228 }
229 static DRIVER_ATTR_WO(delete_device);
230
231 static struct attribute *gpio_aggregator_attrs[] = {
232 &driver_attr_new_device.attr,
233 &driver_attr_delete_device.attr,
234 NULL
235 };
236 ATTRIBUTE_GROUPS(gpio_aggregator);
237
gpio_aggregator_idr_remove(int id,void * p,void * data)238 static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data)
239 {
240 gpio_aggregator_free(p);
241 return 0;
242 }
243
gpio_aggregator_remove_all(void)244 static void __exit gpio_aggregator_remove_all(void)
245 {
246 mutex_lock(&gpio_aggregator_lock);
247 idr_for_each(&gpio_aggregator_idr, gpio_aggregator_idr_remove, NULL);
248 idr_destroy(&gpio_aggregator_idr);
249 mutex_unlock(&gpio_aggregator_lock);
250 }
251
252
253 /*
254 * GPIO Forwarder
255 */
256
257 struct gpiochip_fwd_timing {
258 u32 ramp_up_us;
259 u32 ramp_down_us;
260 };
261
262 struct gpiochip_fwd {
263 struct gpio_chip chip;
264 struct gpio_desc **descs;
265 union {
266 struct mutex mlock; /* protects tmp[] if can_sleep */
267 spinlock_t slock; /* protects tmp[] if !can_sleep */
268 };
269 struct gpiochip_fwd_timing *delay_timings;
270 unsigned long tmp[]; /* values and descs for multiple ops */
271 };
272
273 #define fwd_tmp_values(fwd) &(fwd)->tmp[0]
274 #define fwd_tmp_descs(fwd) (void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
275
276 #define fwd_tmp_size(ngpios) (BITS_TO_LONGS((ngpios)) + (ngpios))
277
gpio_fwd_get_direction(struct gpio_chip * chip,unsigned int offset)278 static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
279 {
280 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
281
282 return gpiod_get_direction(fwd->descs[offset]);
283 }
284
gpio_fwd_direction_input(struct gpio_chip * chip,unsigned int offset)285 static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset)
286 {
287 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
288
289 return gpiod_direction_input(fwd->descs[offset]);
290 }
291
gpio_fwd_direction_output(struct gpio_chip * chip,unsigned int offset,int value)292 static int gpio_fwd_direction_output(struct gpio_chip *chip,
293 unsigned int offset, int value)
294 {
295 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
296
297 return gpiod_direction_output(fwd->descs[offset], value);
298 }
299
gpio_fwd_get(struct gpio_chip * chip,unsigned int offset)300 static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
301 {
302 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
303
304 return chip->can_sleep ? gpiod_get_value_cansleep(fwd->descs[offset])
305 : gpiod_get_value(fwd->descs[offset]);
306 }
307
gpio_fwd_get_multiple(struct gpiochip_fwd * fwd,unsigned long * mask,unsigned long * bits)308 static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
309 unsigned long *bits)
310 {
311 struct gpio_desc **descs = fwd_tmp_descs(fwd);
312 unsigned long *values = fwd_tmp_values(fwd);
313 unsigned int i, j = 0;
314 int error;
315
316 bitmap_clear(values, 0, fwd->chip.ngpio);
317 for_each_set_bit(i, mask, fwd->chip.ngpio)
318 descs[j++] = fwd->descs[i];
319
320 if (fwd->chip.can_sleep)
321 error = gpiod_get_array_value_cansleep(j, descs, NULL, values);
322 else
323 error = gpiod_get_array_value(j, descs, NULL, values);
324 if (error)
325 return error;
326
327 j = 0;
328 for_each_set_bit(i, mask, fwd->chip.ngpio)
329 __assign_bit(i, bits, test_bit(j++, values));
330
331 return 0;
332 }
333
gpio_fwd_get_multiple_locked(struct gpio_chip * chip,unsigned long * mask,unsigned long * bits)334 static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
335 unsigned long *mask, unsigned long *bits)
336 {
337 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
338 unsigned long flags;
339 int error;
340
341 if (chip->can_sleep) {
342 mutex_lock(&fwd->mlock);
343 error = gpio_fwd_get_multiple(fwd, mask, bits);
344 mutex_unlock(&fwd->mlock);
345 } else {
346 spin_lock_irqsave(&fwd->slock, flags);
347 error = gpio_fwd_get_multiple(fwd, mask, bits);
348 spin_unlock_irqrestore(&fwd->slock, flags);
349 }
350
351 return error;
352 }
353
gpio_fwd_delay(struct gpio_chip * chip,unsigned int offset,int value)354 static void gpio_fwd_delay(struct gpio_chip *chip, unsigned int offset, int value)
355 {
356 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
357 const struct gpiochip_fwd_timing *delay_timings;
358 bool is_active_low = gpiod_is_active_low(fwd->descs[offset]);
359 u32 delay_us;
360
361 delay_timings = &fwd->delay_timings[offset];
362 if ((!is_active_low && value) || (is_active_low && !value))
363 delay_us = delay_timings->ramp_up_us;
364 else
365 delay_us = delay_timings->ramp_down_us;
366 if (!delay_us)
367 return;
368
369 if (chip->can_sleep)
370 fsleep(delay_us);
371 else
372 udelay(delay_us);
373 }
374
gpio_fwd_set(struct gpio_chip * chip,unsigned int offset,int value)375 static int gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
376 {
377 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
378 int ret;
379
380 if (chip->can_sleep)
381 ret = gpiod_set_value_cansleep(fwd->descs[offset], value);
382 else
383 ret = gpiod_set_value(fwd->descs[offset], value);
384 if (ret)
385 return ret;
386
387 if (fwd->delay_timings)
388 gpio_fwd_delay(chip, offset, value);
389
390 return ret;
391 }
392
gpio_fwd_set_multiple(struct gpiochip_fwd * fwd,unsigned long * mask,unsigned long * bits)393 static int gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
394 unsigned long *bits)
395 {
396 struct gpio_desc **descs = fwd_tmp_descs(fwd);
397 unsigned long *values = fwd_tmp_values(fwd);
398 unsigned int i, j = 0, ret;
399
400 for_each_set_bit(i, mask, fwd->chip.ngpio) {
401 __assign_bit(j, values, test_bit(i, bits));
402 descs[j++] = fwd->descs[i];
403 }
404
405 if (fwd->chip.can_sleep)
406 ret = gpiod_set_array_value_cansleep(j, descs, NULL, values);
407 else
408 ret = gpiod_set_array_value(j, descs, NULL, values);
409
410 return ret;
411 }
412
gpio_fwd_set_multiple_locked(struct gpio_chip * chip,unsigned long * mask,unsigned long * bits)413 static int gpio_fwd_set_multiple_locked(struct gpio_chip *chip,
414 unsigned long *mask, unsigned long *bits)
415 {
416 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
417 unsigned long flags;
418 int ret;
419
420 if (chip->can_sleep) {
421 mutex_lock(&fwd->mlock);
422 ret = gpio_fwd_set_multiple(fwd, mask, bits);
423 mutex_unlock(&fwd->mlock);
424 } else {
425 spin_lock_irqsave(&fwd->slock, flags);
426 ret = gpio_fwd_set_multiple(fwd, mask, bits);
427 spin_unlock_irqrestore(&fwd->slock, flags);
428 }
429
430 return ret;
431 }
432
gpio_fwd_set_config(struct gpio_chip * chip,unsigned int offset,unsigned long config)433 static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
434 unsigned long config)
435 {
436 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
437
438 return gpiod_set_config(fwd->descs[offset], config);
439 }
440
gpio_fwd_to_irq(struct gpio_chip * chip,unsigned int offset)441 static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset)
442 {
443 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
444
445 return gpiod_to_irq(fwd->descs[offset]);
446 }
447
448 /*
449 * The GPIO delay provides a way to configure platform specific delays
450 * for the GPIO ramp-up or ramp-down delays. This can serve the following
451 * purposes:
452 * - Open-drain output using an RC filter
453 */
454 #define FWD_FEATURE_DELAY BIT(0)
455
456 #ifdef CONFIG_OF_GPIO
gpiochip_fwd_delay_of_xlate(struct gpio_chip * chip,const struct of_phandle_args * gpiospec,u32 * flags)457 static int gpiochip_fwd_delay_of_xlate(struct gpio_chip *chip,
458 const struct of_phandle_args *gpiospec,
459 u32 *flags)
460 {
461 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
462 struct gpiochip_fwd_timing *timings;
463 u32 line;
464
465 if (gpiospec->args_count != chip->of_gpio_n_cells)
466 return -EINVAL;
467
468 line = gpiospec->args[0];
469 if (line >= chip->ngpio)
470 return -EINVAL;
471
472 timings = &fwd->delay_timings[line];
473 timings->ramp_up_us = gpiospec->args[1];
474 timings->ramp_down_us = gpiospec->args[2];
475
476 return line;
477 }
478
gpiochip_fwd_setup_delay_line(struct device * dev,struct gpio_chip * chip,struct gpiochip_fwd * fwd)479 static int gpiochip_fwd_setup_delay_line(struct device *dev, struct gpio_chip *chip,
480 struct gpiochip_fwd *fwd)
481 {
482 fwd->delay_timings = devm_kcalloc(dev, chip->ngpio,
483 sizeof(*fwd->delay_timings),
484 GFP_KERNEL);
485 if (!fwd->delay_timings)
486 return -ENOMEM;
487
488 chip->of_xlate = gpiochip_fwd_delay_of_xlate;
489 chip->of_gpio_n_cells = 3;
490
491 return 0;
492 }
493 #else
gpiochip_fwd_setup_delay_line(struct device * dev,struct gpio_chip * chip,struct gpiochip_fwd * fwd)494 static int gpiochip_fwd_setup_delay_line(struct device *dev, struct gpio_chip *chip,
495 struct gpiochip_fwd *fwd)
496 {
497 return 0;
498 }
499 #endif /* !CONFIG_OF_GPIO */
500
501 /**
502 * gpiochip_fwd_create() - Create a new GPIO forwarder
503 * @dev: Parent device pointer
504 * @ngpios: Number of GPIOs in the forwarder.
505 * @descs: Array containing the GPIO descriptors to forward to.
506 * This array must contain @ngpios entries, and must not be deallocated
507 * before the forwarder has been destroyed again.
508 * @features: Bitwise ORed features as defined with FWD_FEATURE_*.
509 *
510 * This function creates a new gpiochip, which forwards all GPIO operations to
511 * the passed GPIO descriptors.
512 *
513 * Return: An opaque object pointer, or an ERR_PTR()-encoded negative error
514 * code on failure.
515 */
gpiochip_fwd_create(struct device * dev,unsigned int ngpios,struct gpio_desc * descs[],unsigned long features)516 static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
517 unsigned int ngpios,
518 struct gpio_desc *descs[],
519 unsigned long features)
520 {
521 const char *label = dev_name(dev);
522 struct gpiochip_fwd *fwd;
523 struct gpio_chip *chip;
524 unsigned int i;
525 int error;
526
527 fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
528 GFP_KERNEL);
529 if (!fwd)
530 return ERR_PTR(-ENOMEM);
531
532 chip = &fwd->chip;
533
534 /*
535 * If any of the GPIO lines are sleeping, then the entire forwarder
536 * will be sleeping.
537 * If any of the chips support .set_config(), then the forwarder will
538 * support setting configs.
539 */
540 for (i = 0; i < ngpios; i++) {
541 struct gpio_chip *parent = gpiod_to_chip(descs[i]);
542
543 dev_dbg(dev, "%u => gpio %d irq %d\n", i,
544 desc_to_gpio(descs[i]), gpiod_to_irq(descs[i]));
545
546 if (gpiod_cansleep(descs[i]))
547 chip->can_sleep = true;
548 if (parent && parent->set_config)
549 chip->set_config = gpio_fwd_set_config;
550 }
551
552 chip->label = label;
553 chip->parent = dev;
554 chip->owner = THIS_MODULE;
555 chip->get_direction = gpio_fwd_get_direction;
556 chip->direction_input = gpio_fwd_direction_input;
557 chip->direction_output = gpio_fwd_direction_output;
558 chip->get = gpio_fwd_get;
559 chip->get_multiple = gpio_fwd_get_multiple_locked;
560 chip->set_rv = gpio_fwd_set;
561 chip->set_multiple_rv = gpio_fwd_set_multiple_locked;
562 chip->to_irq = gpio_fwd_to_irq;
563 chip->base = -1;
564 chip->ngpio = ngpios;
565 fwd->descs = descs;
566
567 if (chip->can_sleep)
568 mutex_init(&fwd->mlock);
569 else
570 spin_lock_init(&fwd->slock);
571
572 if (features & FWD_FEATURE_DELAY) {
573 error = gpiochip_fwd_setup_delay_line(dev, chip, fwd);
574 if (error)
575 return ERR_PTR(error);
576 }
577
578 error = devm_gpiochip_add_data(dev, chip, fwd);
579 if (error)
580 return ERR_PTR(error);
581
582 return fwd;
583 }
584
585
586 /*
587 * GPIO Aggregator platform device
588 */
589
gpio_aggregator_probe(struct platform_device * pdev)590 static int gpio_aggregator_probe(struct platform_device *pdev)
591 {
592 struct device *dev = &pdev->dev;
593 struct gpio_desc **descs;
594 struct gpiochip_fwd *fwd;
595 unsigned long features;
596 int i, n;
597
598 n = gpiod_count(dev, NULL);
599 if (n < 0)
600 return n;
601
602 descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL);
603 if (!descs)
604 return -ENOMEM;
605
606 for (i = 0; i < n; i++) {
607 descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
608 if (IS_ERR(descs[i]))
609 return PTR_ERR(descs[i]);
610 }
611
612 features = (uintptr_t)device_get_match_data(dev);
613 fwd = gpiochip_fwd_create(dev, n, descs, features);
614 if (IS_ERR(fwd))
615 return PTR_ERR(fwd);
616
617 platform_set_drvdata(pdev, fwd);
618 return 0;
619 }
620
621 static const struct of_device_id gpio_aggregator_dt_ids[] = {
622 {
623 .compatible = "gpio-delay",
624 .data = (void *)FWD_FEATURE_DELAY,
625 },
626 /*
627 * Add GPIO-operated devices controlled from userspace below,
628 * or use "driver_override" in sysfs.
629 */
630 {}
631 };
632 MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
633
634 static struct platform_driver gpio_aggregator_driver = {
635 .probe = gpio_aggregator_probe,
636 .driver = {
637 .name = DRV_NAME,
638 .groups = gpio_aggregator_groups,
639 .of_match_table = gpio_aggregator_dt_ids,
640 },
641 };
642
gpio_aggregator_init(void)643 static int __init gpio_aggregator_init(void)
644 {
645 return platform_driver_register(&gpio_aggregator_driver);
646 }
647 module_init(gpio_aggregator_init);
648
gpio_aggregator_exit(void)649 static void __exit gpio_aggregator_exit(void)
650 {
651 gpio_aggregator_remove_all();
652 platform_driver_unregister(&gpio_aggregator_driver);
653 }
654 module_exit(gpio_aggregator_exit);
655
656 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
657 MODULE_DESCRIPTION("GPIO Aggregator");
658 MODULE_LICENSE("GPL v2");
659