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