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