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