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