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