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