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