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