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