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