^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/gpio/driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kdev_t.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "gpiolib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "gpiolib-sysfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define GPIO_IRQF_TRIGGER_FALLING BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define GPIO_IRQF_TRIGGER_RISING BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define GPIO_IRQF_TRIGGER_BOTH (GPIO_IRQF_TRIGGER_FALLING | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) GPIO_IRQF_TRIGGER_RISING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct gpiod_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct kernfs_node *value_kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) unsigned char irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) bool direction_can_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Lock to serialise gpiod export and unexport, and prevent re-export of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * gpiod whose chip is being unregistered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static DEFINE_MUTEX(sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * /sys/class/gpio/gpioN... only for GPIOs that are exported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * /direction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * * MAY BE OMITTED if kernel won't allow direction changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * * is read/write as "in" or "out"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * * may also be written as "high" or "low", initializing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * output value as specified ("out" implies "low")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * /value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * * always readable, subject to hardware behavior
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * * may be writable, as zero/nonzero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * /edge
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * * configures behavior of poll(2) on /value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * * available only if pin can generate IRQs on input
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * * is read/write as "none", "falling", "rising", or "both"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * /active_low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * * configures polarity of /value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * * is read/write as zero/nonzero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * * also affects existing and subsequent "falling" and "rising"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * /edge configuration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static ssize_t direction_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct gpio_desc *desc = data->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ssize_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) gpiod_get_direction(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) status = sprintf(buf, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) test_bit(FLAG_IS_OUT, &desc->flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) ? "out" : "in");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) static ssize_t direction_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct gpio_desc *desc = data->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ssize_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (sysfs_streq(buf, "high"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) status = gpiod_direction_output_raw(desc, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) status = gpiod_direction_output_raw(desc, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) else if (sysfs_streq(buf, "in"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) status = gpiod_direction_input(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return status ? : size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static DEVICE_ATTR_RW(direction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static ssize_t value_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct gpio_desc *desc = data->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) ssize_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) status = gpiod_get_value_cansleep(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) buf[0] = '0' + status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) buf[1] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) status = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static ssize_t value_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct gpio_desc *desc = data->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ssize_t status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) status = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (size <= 2 && isdigit(buf[0]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) (size == 1 || buf[1] == '\n'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) value = buf[0] - '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) status = kstrtol(buf, 0, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (status == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) gpiod_set_value_cansleep(desc, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) status = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct gpiod_data *data = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) sysfs_notify_dirent(data->value_kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return IRQ_HANDLED;
^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) /* Caller holds gpiod-data mutex. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct gpio_desc *desc = data->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) unsigned long irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) data->irq = gpiod_to_irq(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (data->irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (!data->value_kn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) irq_flags = IRQF_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (flags & GPIO_IRQF_TRIGGER_FALLING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (flags & GPIO_IRQF_TRIGGER_RISING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * FIXME: This should be done in the irq_request_resources callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * when the irq is requested, but a few drivers currently fail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * to do so.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * Remove this redundant call (along with the corresponding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * unlock) when those drivers have been fixed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) ret = gpiochip_lock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) goto err_put_kn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) "gpiolib", data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) data->irq_flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) err_put_kn:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) sysfs_put(data->value_kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return ret;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * Caller holds gpiod-data mutex (unless called after class-device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * deregistration).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static void gpio_sysfs_free_irq(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct gpio_desc *desc = data->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) data->irq_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) free_irq(data->irq, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) sysfs_put(data->value_kn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static const struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) unsigned char flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) } trigger_types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) { "none", 0 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) { "falling", GPIO_IRQF_TRIGGER_FALLING },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) { "rising", GPIO_IRQF_TRIGGER_RISING },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) { "both", GPIO_IRQF_TRIGGER_BOTH },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) static ssize_t edge_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) ssize_t status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (data->irq_flags == trigger_types[i].flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) status = sprintf(buf, "%s\n", trigger_types[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static ssize_t edge_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) unsigned char flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ssize_t status = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) for (i = 0; i < ARRAY_SIZE(trigger_types); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (sysfs_streq(trigger_types[i].name, buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (i == ARRAY_SIZE(trigger_types))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) flags = trigger_types[i].flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (flags == data->irq_flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) status = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (data->irq_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) gpio_sysfs_free_irq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) status = gpio_sysfs_request_irq(dev, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (!status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) status = size;
^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) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) static DEVICE_ATTR_RW(edge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /* Caller holds gpiod-data mutex. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static int gpio_sysfs_set_active_low(struct device *dev, int value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) struct gpio_desc *desc = data->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) unsigned int flags = data->irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) set_bit(FLAG_ACTIVE_LOW, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) clear_bit(FLAG_ACTIVE_LOW, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /* reconfigure poll(2) support if enabled on one edge only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (flags == GPIO_IRQF_TRIGGER_FALLING ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) flags == GPIO_IRQF_TRIGGER_RISING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) gpio_sysfs_free_irq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) status = gpio_sysfs_request_irq(dev, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static ssize_t active_low_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct gpio_desc *desc = data->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) ssize_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) status = sprintf(buf, "%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) !!test_bit(FLAG_ACTIVE_LOW, &desc->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) static ssize_t active_low_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct device_attribute *attr, const char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) ssize_t status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) long value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) mutex_lock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) status = kstrtol(buf, 0, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (status == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) status = gpio_sysfs_set_active_low(dev, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) mutex_unlock(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return status ? : size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static DEVICE_ATTR_RW(active_low);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct device *dev = kobj_to_dev(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct gpio_desc *desc = data->desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) umode_t mode = attr->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) bool show_direction = data->direction_can_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (attr == &dev_attr_direction.attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (!show_direction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) } else if (attr == &dev_attr_edge.attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (gpiod_to_irq(desc) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) static struct attribute *gpio_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) &dev_attr_direction.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) &dev_attr_edge.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) &dev_attr_value.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) &dev_attr_active_low.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) static const struct attribute_group gpio_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) .attrs = gpio_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) .is_visible = gpio_is_visible,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static const struct attribute_group *gpio_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) &gpio_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) * /sys/class/gpio/gpiochipN/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * /base ... matching gpio_chip.base (N)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * /label ... matching gpio_chip.label
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * /ngpio ... matching gpio_chip.ngpio
^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) static ssize_t base_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) const struct gpio_chip *chip = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return sprintf(buf, "%d\n", chip->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static DEVICE_ATTR_RO(base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) static ssize_t label_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) const struct gpio_chip *chip = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return sprintf(buf, "%s\n", chip->label ? : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static DEVICE_ATTR_RO(label);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) static ssize_t ngpio_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) const struct gpio_chip *chip = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) return sprintf(buf, "%u\n", chip->ngpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) static DEVICE_ATTR_RO(ngpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) static struct attribute *gpiochip_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) &dev_attr_base.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) &dev_attr_label.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) &dev_attr_ngpio.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) ATTRIBUTE_GROUPS(gpiochip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) * /sys/class/gpio/export ... write-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * integer N ... number of GPIO to export (full access)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * /sys/class/gpio/unexport ... write-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * integer N ... number of GPIO to unexport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) static ssize_t export_store(struct class *class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) struct class_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) long gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) struct gpio_chip *gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) status = kstrtol(buf, 0, &gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) desc = gpio_to_desc(gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) /* reject invalid GPIOs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (!desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) gc = desc->gdev->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) offset = gpio_chip_hwgpio(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (!gpiochip_line_is_valid(gc, offset)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) pr_warn("%s: GPIO %ld masked\n", __func__, gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) /* No extra locking here; FLAG_SYSFS just signifies that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) * request and export were done by on behalf of userspace, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) * they may be undone on its behalf too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) status = gpiod_request(desc, "sysfs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (status == -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) status = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) status = gpiod_set_transitory(desc, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (!status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) status = gpiod_export(desc, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) gpiod_free(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) set_bit(FLAG_SYSFS, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) pr_debug("%s: status %d\n", __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return status ? : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) static CLASS_ATTR_WO(export);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) static ssize_t unexport_store(struct class *class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) struct class_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) const char *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) long gpio;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) status = kstrtol(buf, 0, &gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) desc = gpio_to_desc(gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) /* reject bogus commands (gpio_unexport ignores them) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) if (!desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) status = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /* No extra locking here; FLAG_SYSFS just signifies that the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * request and export were done by on behalf of userspace, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * they may be undone on its behalf too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) gpiod_free(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) pr_debug("%s: status %d\n", __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) return status ? : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) static CLASS_ATTR_WO(unexport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) static struct attribute *gpio_class_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) &class_attr_export.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) &class_attr_unexport.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) ATTRIBUTE_GROUPS(gpio_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static struct class gpio_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) .name = "gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) .class_groups = gpio_class_groups,
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * gpiod_export - export a GPIO through sysfs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * @desc: GPIO to make available, already requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) * @direction_may_change: true if userspace may change GPIO direction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) * Context: arch_initcall or later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) * When drivers want to make a GPIO accessible to userspace after they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) * have requested it -- perhaps while debugging, or as part of their
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * public interface -- they may use this routine. If the GPIO can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * change direction (some can't) and the caller allows it, userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * will see "direction" sysfs attribute which may be used to change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * the gpio's direction. A "value" attribute will always be provided.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * Returns zero on success, else an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) struct gpio_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) struct gpio_device *gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) struct gpiod_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) const char *ioname = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) /* can't export until sysfs is available ... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (!gpio_class.p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) pr_debug("%s: called too early!\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) if (!desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) pr_debug("%s: invalid gpio descriptor\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) gdev = desc->gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) chip = gdev->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) mutex_lock(&sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) /* check if chip is being removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (!chip || !gdev->mockdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) status = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) spin_lock_irqsave(&gpio_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) test_bit(FLAG_EXPORT, &desc->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) spin_unlock_irqrestore(&gpio_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) test_bit(FLAG_REQUESTED, &desc->flags),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) test_bit(FLAG_EXPORT, &desc->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) status = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) spin_unlock_irqrestore(&gpio_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) data = kzalloc(sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) status = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) data->desc = desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) mutex_init(&data->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (chip->direction_input && chip->direction_output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) data->direction_can_change = direction_may_change;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) data->direction_can_change = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) offset = gpio_chip_hwgpio(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) if (chip->names && chip->names[offset])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) ioname = chip->names[offset];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) dev = device_create_with_groups(&gpio_class, &gdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) MKDEV(0, 0), data, gpio_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) ioname ? ioname : "gpio%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) desc_to_gpio(desc));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (IS_ERR(dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) status = PTR_ERR(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) goto err_free_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) set_bit(FLAG_EXPORT, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) mutex_unlock(&sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) err_free_data:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) mutex_unlock(&sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) gpiod_dbg(desc, "%s: status %d\n", __func__, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) EXPORT_SYMBOL_GPL(gpiod_export);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) static int match_export(struct device *dev, const void *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) struct gpiod_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) return data->desc == desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) * gpiod_export_link - create a sysfs link to an exported GPIO node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) * @dev: device under which to create symlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) * @name: name of the symlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) * @desc: GPIO to create symlink to, already exported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) * node. Caller is responsible for unlinking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) * Returns zero on success, else an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) int gpiod_export_link(struct device *dev, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) struct gpio_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) struct device *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (!desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) pr_warn("%s: invalid GPIO\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) cdev = class_find_device(&gpio_class, NULL, desc, match_export);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) put_device(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) EXPORT_SYMBOL_GPL(gpiod_export_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) * gpiod_unexport - reverse effect of gpiod_export()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) * @desc: GPIO to make unavailable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * This is implicit on gpiod_free().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) void gpiod_unexport(struct gpio_desc *desc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) struct gpiod_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (!desc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) pr_warn("%s: invalid GPIO\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) mutex_lock(&sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (!test_bit(FLAG_EXPORT, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) dev = class_find_device(&gpio_class, NULL, desc, match_export);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) goto err_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) clear_bit(FLAG_EXPORT, &desc->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) device_unregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) * Release irq after deregistration to prevent race with edge_store.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) if (data->irq_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) gpio_sysfs_free_irq(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) mutex_unlock(&sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) err_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) mutex_unlock(&sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) EXPORT_SYMBOL_GPL(gpiod_unexport);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) int gpiochip_sysfs_register(struct gpio_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) struct device *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) struct gpio_chip *chip = gdev->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) * Many systems add gpio chips for SOC support very early,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) * before driver model support is available. In those cases we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) * register later, in gpiolib_sysfs_init() ... here we just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) * verify that _some_ field of gpio_class got initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (!gpio_class.p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) * For sysfs backward compatibility we need to preserve this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) * preferred parenting to the gpio_chip parent field, if set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (chip->parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) parent = chip->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) parent = &gdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) /* use chip->base for the ID; it's already known to be unique */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) gpiochip_groups, GPIOCHIP_NAME "%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) chip->base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (IS_ERR(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) return PTR_ERR(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) mutex_lock(&sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) gdev->mockdev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) mutex_unlock(&sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) void gpiochip_sysfs_unregister(struct gpio_device *gdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) struct gpio_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) struct gpio_chip *chip = gdev->chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (!gdev->mockdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) device_unregister(gdev->mockdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) /* prevent further gpiod exports */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) mutex_lock(&sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) gdev->mockdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) mutex_unlock(&sysfs_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) /* unregister gpiod class devices owned by sysfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) for (i = 0; i < chip->ngpio; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) desc = &gdev->descs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) if (test_and_clear_bit(FLAG_SYSFS, &desc->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) gpiod_free(desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) static int __init gpiolib_sysfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) struct gpio_device *gdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) status = class_register(&gpio_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) if (status < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) /* Scan and register the gpio_chips which registered very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) * early (e.g. before the class_register above was called).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) * We run before arch_initcall() so chip->dev nodes can have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * registered, and so arch_initcall() can always gpio_export().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) spin_lock_irqsave(&gpio_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) list_for_each_entry(gdev, &gpio_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) if (gdev->mockdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) * TODO we yield gpio_lock here because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) * gpiochip_sysfs_register() acquires a mutex. This is unsafe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) * and needs to be fixed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) * Also it would be nice to use gpiochip_find() here so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) * can keep gpio_chips local to gpiolib.c, but the yield of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) * gpio_lock prevents us from doing this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) spin_unlock_irqrestore(&gpio_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) status = gpiochip_sysfs_register(gdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) spin_lock_irqsave(&gpio_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) spin_unlock_irqrestore(&gpio_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) postcore_initcall(gpiolib_sysfs_init);