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