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