0001
0002
0003
0004
0005
0006
0007 #define DRV_NAME "gpio-aggregator"
0008 #define pr_fmt(fmt) DRV_NAME ": " fmt
0009
0010 #include <linux/bitmap.h>
0011 #include <linux/bitops.h>
0012 #include <linux/ctype.h>
0013 #include <linux/gpio.h>
0014 #include <linux/gpio/consumer.h>
0015 #include <linux/gpio/driver.h>
0016 #include <linux/gpio/machine.h>
0017 #include <linux/idr.h>
0018 #include <linux/kernel.h>
0019 #include <linux/module.h>
0020 #include <linux/mutex.h>
0021 #include <linux/overflow.h>
0022 #include <linux/platform_device.h>
0023 #include <linux/spinlock.h>
0024 #include <linux/string.h>
0025
0026
0027
0028
0029
0030
0031 struct gpio_aggregator {
0032 struct gpiod_lookup_table *lookups;
0033 struct platform_device *pdev;
0034 char args[];
0035 };
0036
0037 static DEFINE_MUTEX(gpio_aggregator_lock);
0038 static DEFINE_IDR(gpio_aggregator_idr);
0039
0040 static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key,
0041 int hwnum, unsigned int *n)
0042 {
0043 struct gpiod_lookup_table *lookups;
0044
0045 lookups = krealloc(aggr->lookups, struct_size(lookups, table, *n + 2),
0046 GFP_KERNEL);
0047 if (!lookups)
0048 return -ENOMEM;
0049
0050 lookups->table[*n] = GPIO_LOOKUP_IDX(key, hwnum, NULL, *n, 0);
0051
0052 (*n)++;
0053 memset(&lookups->table[*n], 0, sizeof(lookups->table[*n]));
0054
0055 aggr->lookups = lookups;
0056 return 0;
0057 }
0058
0059 static int aggr_parse(struct gpio_aggregator *aggr)
0060 {
0061 char *args = skip_spaces(aggr->args);
0062 char *name, *offsets, *p;
0063 unsigned long *bitmap;
0064 unsigned int i, n = 0;
0065 int error = 0;
0066
0067 bitmap = bitmap_alloc(ARCH_NR_GPIOS, GFP_KERNEL);
0068 if (!bitmap)
0069 return -ENOMEM;
0070
0071 args = next_arg(args, &name, &p);
0072 while (*args) {
0073 args = next_arg(args, &offsets, &p);
0074
0075 p = get_options(offsets, 0, &error);
0076 if (error == 0 || *p) {
0077
0078 error = aggr_add_gpio(aggr, name, U16_MAX, &n);
0079 if (error)
0080 goto free_bitmap;
0081
0082 name = offsets;
0083 continue;
0084 }
0085
0086
0087 error = bitmap_parselist(offsets, bitmap, ARCH_NR_GPIOS);
0088 if (error) {
0089 pr_err("Cannot parse %s: %d\n", offsets, error);
0090 goto free_bitmap;
0091 }
0092
0093 for_each_set_bit(i, bitmap, ARCH_NR_GPIOS) {
0094 error = aggr_add_gpio(aggr, name, i, &n);
0095 if (error)
0096 goto free_bitmap;
0097 }
0098
0099 args = next_arg(args, &name, &p);
0100 }
0101
0102 if (!n) {
0103 pr_err("No GPIOs specified\n");
0104 error = -EINVAL;
0105 }
0106
0107 free_bitmap:
0108 bitmap_free(bitmap);
0109 return error;
0110 }
0111
0112 static ssize_t new_device_store(struct device_driver *driver, const char *buf,
0113 size_t count)
0114 {
0115 struct gpio_aggregator *aggr;
0116 struct platform_device *pdev;
0117 int res, id;
0118
0119
0120 aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL);
0121 if (!aggr)
0122 return -ENOMEM;
0123
0124 memcpy(aggr->args, buf, count + 1);
0125
0126 aggr->lookups = kzalloc(struct_size(aggr->lookups, table, 1),
0127 GFP_KERNEL);
0128 if (!aggr->lookups) {
0129 res = -ENOMEM;
0130 goto free_ga;
0131 }
0132
0133 mutex_lock(&gpio_aggregator_lock);
0134 id = idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL);
0135 mutex_unlock(&gpio_aggregator_lock);
0136
0137 if (id < 0) {
0138 res = id;
0139 goto free_table;
0140 }
0141
0142 aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id);
0143 if (!aggr->lookups->dev_id) {
0144 res = -ENOMEM;
0145 goto remove_idr;
0146 }
0147
0148 res = aggr_parse(aggr);
0149 if (res)
0150 goto free_dev_id;
0151
0152 gpiod_add_lookup_table(aggr->lookups);
0153
0154 pdev = platform_device_register_simple(DRV_NAME, id, NULL, 0);
0155 if (IS_ERR(pdev)) {
0156 res = PTR_ERR(pdev);
0157 goto remove_table;
0158 }
0159
0160 aggr->pdev = pdev;
0161 return count;
0162
0163 remove_table:
0164 gpiod_remove_lookup_table(aggr->lookups);
0165 free_dev_id:
0166 kfree(aggr->lookups->dev_id);
0167 remove_idr:
0168 mutex_lock(&gpio_aggregator_lock);
0169 idr_remove(&gpio_aggregator_idr, id);
0170 mutex_unlock(&gpio_aggregator_lock);
0171 free_table:
0172 kfree(aggr->lookups);
0173 free_ga:
0174 kfree(aggr);
0175 return res;
0176 }
0177
0178 static DRIVER_ATTR_WO(new_device);
0179
0180 static void gpio_aggregator_free(struct gpio_aggregator *aggr)
0181 {
0182 platform_device_unregister(aggr->pdev);
0183 gpiod_remove_lookup_table(aggr->lookups);
0184 kfree(aggr->lookups->dev_id);
0185 kfree(aggr->lookups);
0186 kfree(aggr);
0187 }
0188
0189 static ssize_t delete_device_store(struct device_driver *driver,
0190 const char *buf, size_t count)
0191 {
0192 struct gpio_aggregator *aggr;
0193 unsigned int id;
0194 int error;
0195
0196 if (!str_has_prefix(buf, DRV_NAME "."))
0197 return -EINVAL;
0198
0199 error = kstrtouint(buf + strlen(DRV_NAME "."), 10, &id);
0200 if (error)
0201 return error;
0202
0203 mutex_lock(&gpio_aggregator_lock);
0204 aggr = idr_remove(&gpio_aggregator_idr, id);
0205 mutex_unlock(&gpio_aggregator_lock);
0206 if (!aggr)
0207 return -ENOENT;
0208
0209 gpio_aggregator_free(aggr);
0210 return count;
0211 }
0212 static DRIVER_ATTR_WO(delete_device);
0213
0214 static struct attribute *gpio_aggregator_attrs[] = {
0215 &driver_attr_new_device.attr,
0216 &driver_attr_delete_device.attr,
0217 NULL
0218 };
0219 ATTRIBUTE_GROUPS(gpio_aggregator);
0220
0221 static int __exit gpio_aggregator_idr_remove(int id, void *p, void *data)
0222 {
0223 gpio_aggregator_free(p);
0224 return 0;
0225 }
0226
0227 static void __exit gpio_aggregator_remove_all(void)
0228 {
0229 mutex_lock(&gpio_aggregator_lock);
0230 idr_for_each(&gpio_aggregator_idr, gpio_aggregator_idr_remove, NULL);
0231 idr_destroy(&gpio_aggregator_idr);
0232 mutex_unlock(&gpio_aggregator_lock);
0233 }
0234
0235
0236
0237
0238
0239
0240 struct gpiochip_fwd {
0241 struct gpio_chip chip;
0242 struct gpio_desc **descs;
0243 union {
0244 struct mutex mlock;
0245 spinlock_t slock;
0246 };
0247 unsigned long tmp[];
0248 };
0249
0250 #define fwd_tmp_values(fwd) &(fwd)->tmp[0]
0251 #define fwd_tmp_descs(fwd) (void *)&(fwd)->tmp[BITS_TO_LONGS((fwd)->chip.ngpio)]
0252
0253 #define fwd_tmp_size(ngpios) (BITS_TO_LONGS((ngpios)) + (ngpios))
0254
0255 static int gpio_fwd_get_direction(struct gpio_chip *chip, unsigned int offset)
0256 {
0257 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
0258
0259 return gpiod_get_direction(fwd->descs[offset]);
0260 }
0261
0262 static int gpio_fwd_direction_input(struct gpio_chip *chip, unsigned int offset)
0263 {
0264 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
0265
0266 return gpiod_direction_input(fwd->descs[offset]);
0267 }
0268
0269 static int gpio_fwd_direction_output(struct gpio_chip *chip,
0270 unsigned int offset, int value)
0271 {
0272 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
0273
0274 return gpiod_direction_output(fwd->descs[offset], value);
0275 }
0276
0277 static int gpio_fwd_get(struct gpio_chip *chip, unsigned int offset)
0278 {
0279 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
0280
0281 return chip->can_sleep ? gpiod_get_value_cansleep(fwd->descs[offset])
0282 : gpiod_get_value(fwd->descs[offset]);
0283 }
0284
0285 static int gpio_fwd_get_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
0286 unsigned long *bits)
0287 {
0288 struct gpio_desc **descs = fwd_tmp_descs(fwd);
0289 unsigned long *values = fwd_tmp_values(fwd);
0290 unsigned int i, j = 0;
0291 int error;
0292
0293 bitmap_clear(values, 0, fwd->chip.ngpio);
0294 for_each_set_bit(i, mask, fwd->chip.ngpio)
0295 descs[j++] = fwd->descs[i];
0296
0297 if (fwd->chip.can_sleep)
0298 error = gpiod_get_array_value_cansleep(j, descs, NULL, values);
0299 else
0300 error = gpiod_get_array_value(j, descs, NULL, values);
0301 if (error)
0302 return error;
0303
0304 j = 0;
0305 for_each_set_bit(i, mask, fwd->chip.ngpio)
0306 __assign_bit(i, bits, test_bit(j++, values));
0307
0308 return 0;
0309 }
0310
0311 static int gpio_fwd_get_multiple_locked(struct gpio_chip *chip,
0312 unsigned long *mask, unsigned long *bits)
0313 {
0314 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
0315 unsigned long flags;
0316 int error;
0317
0318 if (chip->can_sleep) {
0319 mutex_lock(&fwd->mlock);
0320 error = gpio_fwd_get_multiple(fwd, mask, bits);
0321 mutex_unlock(&fwd->mlock);
0322 } else {
0323 spin_lock_irqsave(&fwd->slock, flags);
0324 error = gpio_fwd_get_multiple(fwd, mask, bits);
0325 spin_unlock_irqrestore(&fwd->slock, flags);
0326 }
0327
0328 return error;
0329 }
0330
0331 static void gpio_fwd_set(struct gpio_chip *chip, unsigned int offset, int value)
0332 {
0333 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
0334
0335 if (chip->can_sleep)
0336 gpiod_set_value_cansleep(fwd->descs[offset], value);
0337 else
0338 gpiod_set_value(fwd->descs[offset], value);
0339 }
0340
0341 static void gpio_fwd_set_multiple(struct gpiochip_fwd *fwd, unsigned long *mask,
0342 unsigned long *bits)
0343 {
0344 struct gpio_desc **descs = fwd_tmp_descs(fwd);
0345 unsigned long *values = fwd_tmp_values(fwd);
0346 unsigned int i, j = 0;
0347
0348 for_each_set_bit(i, mask, fwd->chip.ngpio) {
0349 __assign_bit(j, values, test_bit(i, bits));
0350 descs[j++] = fwd->descs[i];
0351 }
0352
0353 if (fwd->chip.can_sleep)
0354 gpiod_set_array_value_cansleep(j, descs, NULL, values);
0355 else
0356 gpiod_set_array_value(j, descs, NULL, values);
0357 }
0358
0359 static void gpio_fwd_set_multiple_locked(struct gpio_chip *chip,
0360 unsigned long *mask, unsigned long *bits)
0361 {
0362 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
0363 unsigned long flags;
0364
0365 if (chip->can_sleep) {
0366 mutex_lock(&fwd->mlock);
0367 gpio_fwd_set_multiple(fwd, mask, bits);
0368 mutex_unlock(&fwd->mlock);
0369 } else {
0370 spin_lock_irqsave(&fwd->slock, flags);
0371 gpio_fwd_set_multiple(fwd, mask, bits);
0372 spin_unlock_irqrestore(&fwd->slock, flags);
0373 }
0374 }
0375
0376 static int gpio_fwd_set_config(struct gpio_chip *chip, unsigned int offset,
0377 unsigned long config)
0378 {
0379 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
0380
0381 return gpiod_set_config(fwd->descs[offset], config);
0382 }
0383
0384 static int gpio_fwd_to_irq(struct gpio_chip *chip, unsigned int offset)
0385 {
0386 struct gpiochip_fwd *fwd = gpiochip_get_data(chip);
0387
0388 return gpiod_to_irq(fwd->descs[offset]);
0389 }
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405 static struct gpiochip_fwd *gpiochip_fwd_create(struct device *dev,
0406 unsigned int ngpios,
0407 struct gpio_desc *descs[])
0408 {
0409 const char *label = dev_name(dev);
0410 struct gpiochip_fwd *fwd;
0411 struct gpio_chip *chip;
0412 unsigned int i;
0413 int error;
0414
0415 fwd = devm_kzalloc(dev, struct_size(fwd, tmp, fwd_tmp_size(ngpios)),
0416 GFP_KERNEL);
0417 if (!fwd)
0418 return ERR_PTR(-ENOMEM);
0419
0420 chip = &fwd->chip;
0421
0422
0423
0424
0425
0426
0427
0428 for (i = 0; i < ngpios; i++) {
0429 struct gpio_chip *parent = gpiod_to_chip(descs[i]);
0430
0431 dev_dbg(dev, "%u => gpio %d irq %d\n", i,
0432 desc_to_gpio(descs[i]), gpiod_to_irq(descs[i]));
0433
0434 if (gpiod_cansleep(descs[i]))
0435 chip->can_sleep = true;
0436 if (parent && parent->set_config)
0437 chip->set_config = gpio_fwd_set_config;
0438 }
0439
0440 chip->label = label;
0441 chip->parent = dev;
0442 chip->owner = THIS_MODULE;
0443 chip->get_direction = gpio_fwd_get_direction;
0444 chip->direction_input = gpio_fwd_direction_input;
0445 chip->direction_output = gpio_fwd_direction_output;
0446 chip->get = gpio_fwd_get;
0447 chip->get_multiple = gpio_fwd_get_multiple_locked;
0448 chip->set = gpio_fwd_set;
0449 chip->set_multiple = gpio_fwd_set_multiple_locked;
0450 chip->to_irq = gpio_fwd_to_irq;
0451 chip->base = -1;
0452 chip->ngpio = ngpios;
0453 fwd->descs = descs;
0454
0455 if (chip->can_sleep)
0456 mutex_init(&fwd->mlock);
0457 else
0458 spin_lock_init(&fwd->slock);
0459
0460 error = devm_gpiochip_add_data(dev, chip, fwd);
0461 if (error)
0462 return ERR_PTR(error);
0463
0464 return fwd;
0465 }
0466
0467
0468
0469
0470
0471
0472 static int gpio_aggregator_probe(struct platform_device *pdev)
0473 {
0474 struct device *dev = &pdev->dev;
0475 struct gpio_desc **descs;
0476 struct gpiochip_fwd *fwd;
0477 int i, n;
0478
0479 n = gpiod_count(dev, NULL);
0480 if (n < 0)
0481 return n;
0482
0483 descs = devm_kmalloc_array(dev, n, sizeof(*descs), GFP_KERNEL);
0484 if (!descs)
0485 return -ENOMEM;
0486
0487 for (i = 0; i < n; i++) {
0488 descs[i] = devm_gpiod_get_index(dev, NULL, i, GPIOD_ASIS);
0489 if (IS_ERR(descs[i]))
0490 return PTR_ERR(descs[i]);
0491 }
0492
0493 fwd = gpiochip_fwd_create(dev, n, descs);
0494 if (IS_ERR(fwd))
0495 return PTR_ERR(fwd);
0496
0497 platform_set_drvdata(pdev, fwd);
0498 return 0;
0499 }
0500
0501 #ifdef CONFIG_OF
0502 static const struct of_device_id gpio_aggregator_dt_ids[] = {
0503
0504
0505
0506
0507 {}
0508 };
0509 MODULE_DEVICE_TABLE(of, gpio_aggregator_dt_ids);
0510 #endif
0511
0512 static struct platform_driver gpio_aggregator_driver = {
0513 .probe = gpio_aggregator_probe,
0514 .driver = {
0515 .name = DRV_NAME,
0516 .groups = gpio_aggregator_groups,
0517 .of_match_table = of_match_ptr(gpio_aggregator_dt_ids),
0518 },
0519 };
0520
0521 static int __init gpio_aggregator_init(void)
0522 {
0523 return platform_driver_register(&gpio_aggregator_driver);
0524 }
0525 module_init(gpio_aggregator_init);
0526
0527 static void __exit gpio_aggregator_exit(void)
0528 {
0529 gpio_aggregator_remove_all();
0530 platform_driver_unregister(&gpio_aggregator_driver);
0531 }
0532 module_exit(gpio_aggregator_exit);
0533
0534 MODULE_AUTHOR("Geert Uytterhoeven <geert+renesas@glider.be>");
0535 MODULE_DESCRIPTION("GPIO Aggregator");
0536 MODULE_LICENSE("GPL v2");