Commit | Line | Data |
---|---|---|
d2876d08 DB |
1 | #include <linux/kernel.h> |
2 | #include <linux/module.h> | |
ff77c352 | 3 | #include <linux/interrupt.h> |
d2876d08 DB |
4 | #include <linux/irq.h> |
5 | #include <linux/spinlock.h> | |
1a989d0f | 6 | #include <linux/list.h> |
d8f388d8 DB |
7 | #include <linux/device.h> |
8 | #include <linux/err.h> | |
9 | #include <linux/debugfs.h> | |
10 | #include <linux/seq_file.h> | |
11 | #include <linux/gpio.h> | |
391c970c | 12 | #include <linux/of_gpio.h> |
81f59e9d | 13 | #include <linux/acpi_gpio.h> |
ff77c352 | 14 | #include <linux/idr.h> |
5a0e3ad6 | 15 | #include <linux/slab.h> |
7b199811 | 16 | #include <linux/acpi.h> |
53e7cac3 | 17 | #include <linux/gpio/driver.h> |
d2876d08 | 18 | |
3f397c21 UKK |
19 | #define CREATE_TRACE_POINTS |
20 | #include <trace/events/gpio.h> | |
d2876d08 | 21 | |
79a9becd | 22 | /* Implementation infrastructure for GPIO interfaces. |
d2876d08 | 23 | * |
79a9becd AC |
24 | * The GPIO programming interface allows for inlining speed-critical |
25 | * get/set operations for common cases, so that access to SOC-integrated | |
26 | * GPIOs can sometimes cost only an instruction or two per bit. | |
d2876d08 DB |
27 | */ |
28 | ||
29 | ||
30 | /* When debugging, extend minimal trust to callers and platform code. | |
31 | * Also emit diagnostic messages that may help initial bringup, when | |
32 | * board setup or driver bugs are most common. | |
33 | * | |
34 | * Otherwise, minimize overhead in what may be bitbanging codepaths. | |
35 | */ | |
36 | #ifdef DEBUG | |
37 | #define extra_checks 1 | |
38 | #else | |
39 | #define extra_checks 0 | |
40 | #endif | |
41 | ||
42 | /* gpio_lock prevents conflicts during gpio_desc[] table updates. | |
43 | * While any GPIO is requested, its gpio_chip is not removable; | |
44 | * each GPIO's "requested" flag serves as a lock and refcount. | |
45 | */ | |
46 | static DEFINE_SPINLOCK(gpio_lock); | |
47 | ||
48 | struct gpio_desc { | |
49 | struct gpio_chip *chip; | |
50 | unsigned long flags; | |
51 | /* flag symbols are bit numbers */ | |
52 | #define FLAG_REQUESTED 0 | |
53 | #define FLAG_IS_OUT 1 | |
710b40ea AC |
54 | #define FLAG_EXPORT 2 /* protected by sysfs_lock */ |
55 | #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ | |
56 | #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ | |
57 | #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ | |
79a9becd | 58 | #define FLAG_ACTIVE_LOW 6 /* value has active low */ |
710b40ea AC |
59 | #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ |
60 | #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ | |
d468bf9e | 61 | #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ |
ff77c352 | 62 | |
5ba1821d | 63 | #define ID_SHIFT 16 /* add new flags before this one */ |
ff77c352 | 64 | |
5ba1821d | 65 | #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) |
ff77c352 | 66 | #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) |
d2876d08 DB |
67 | |
68 | #ifdef CONFIG_DEBUG_FS | |
69 | const char *label; | |
70 | #endif | |
71 | }; | |
72 | static struct gpio_desc gpio_desc[ARCH_NR_GPIOS]; | |
73 | ||
6c0b4e6c AC |
74 | #define GPIO_OFFSET_VALID(chip, offset) (offset >= 0 && offset < chip->ngpio) |
75 | ||
bae48da2 AC |
76 | static DEFINE_MUTEX(gpio_lookup_lock); |
77 | static LIST_HEAD(gpio_lookup_list); | |
1a989d0f AC |
78 | static LIST_HEAD(gpio_chips); |
79 | ||
ff77c352 | 80 | #ifdef CONFIG_GPIO_SYSFS |
5ba1821d | 81 | static DEFINE_IDR(dirent_idr); |
ff77c352 DG |
82 | #endif |
83 | ||
372e722e AC |
84 | static int gpiod_request(struct gpio_desc *desc, const char *label); |
85 | static void gpiod_free(struct gpio_desc *desc); | |
372e722e | 86 | |
7b17b59f | 87 | #ifdef CONFIG_DEBUG_FS |
7589e59f AS |
88 | #define gpiod_emerg(desc, fmt, ...) \ |
89 | pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ | |
7b17b59f | 90 | ##__VA_ARGS__) |
7589e59f AS |
91 | #define gpiod_crit(desc, fmt, ...) \ |
92 | pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
7b17b59f | 93 | ##__VA_ARGS__) |
7589e59f AS |
94 | #define gpiod_err(desc, fmt, ...) \ |
95 | pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
7b17b59f | 96 | ##__VA_ARGS__) |
7589e59f AS |
97 | #define gpiod_warn(desc, fmt, ...) \ |
98 | pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
7b17b59f | 99 | ##__VA_ARGS__) |
7589e59f AS |
100 | #define gpiod_info(desc, fmt, ...) \ |
101 | pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ | |
7b17b59f | 102 | ##__VA_ARGS__) |
7589e59f AS |
103 | #define gpiod_dbg(desc, fmt, ...) \ |
104 | pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ | |
7b17b59f MB |
105 | ##__VA_ARGS__) |
106 | #else | |
7589e59f | 107 | #define gpiod_emerg(desc, fmt, ...) \ |
6424de5a | 108 | pr_emerg("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7589e59f | 109 | #define gpiod_crit(desc, fmt, ...) \ |
6424de5a | 110 | pr_crit("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7589e59f | 111 | #define gpiod_err(desc, fmt, ...) \ |
6424de5a | 112 | pr_err("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7589e59f | 113 | #define gpiod_warn(desc, fmt, ...) \ |
6424de5a | 114 | pr_warn("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7589e59f | 115 | #define gpiod_info(desc, fmt, ...) \ |
6424de5a | 116 | pr_info("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7589e59f | 117 | #define gpiod_dbg(desc, fmt, ...) \ |
6424de5a | 118 | pr_debug("gpio-%d: " fmt, desc_to_gpio(desc), ##__VA_ARGS__) |
7b17b59f | 119 | #endif |
372e722e | 120 | |
d2876d08 DB |
121 | static inline void desc_set_label(struct gpio_desc *d, const char *label) |
122 | { | |
123 | #ifdef CONFIG_DEBUG_FS | |
124 | d->label = label; | |
125 | #endif | |
126 | } | |
127 | ||
372e722e AC |
128 | /* |
129 | * Return the GPIO number of the passed descriptor relative to its chip | |
130 | */ | |
131 | static int gpio_chip_hwgpio(const struct gpio_desc *desc) | |
132 | { | |
6c0b4e6c | 133 | return desc - &desc->chip->desc[0]; |
372e722e AC |
134 | } |
135 | ||
136 | /** | |
137 | * Convert a GPIO number to its descriptor | |
138 | */ | |
79a9becd | 139 | struct gpio_desc *gpio_to_desc(unsigned gpio) |
372e722e AC |
140 | { |
141 | if (WARN(!gpio_is_valid(gpio), "invalid GPIO %d\n", gpio)) | |
142 | return NULL; | |
143 | else | |
144 | return &gpio_desc[gpio]; | |
145 | } | |
79a9becd | 146 | EXPORT_SYMBOL_GPL(gpio_to_desc); |
372e722e | 147 | |
d468bf9e LW |
148 | /** |
149 | * Convert an offset on a certain chip to a corresponding descriptor | |
150 | */ | |
151 | static struct gpio_desc *gpiochip_offset_to_desc(struct gpio_chip *chip, | |
152 | unsigned int offset) | |
153 | { | |
b7d0a28a AC |
154 | if (offset >= chip->ngpio) |
155 | return ERR_PTR(-EINVAL); | |
d468bf9e | 156 | |
b7d0a28a | 157 | return &chip->desc[offset]; |
d468bf9e | 158 | } |
372e722e AC |
159 | |
160 | /** | |
161 | * Convert a GPIO descriptor to the integer namespace. | |
162 | * This should disappear in the future but is needed since we still | |
163 | * use GPIO numbers for error messages and sysfs nodes | |
164 | */ | |
79a9becd | 165 | int desc_to_gpio(const struct gpio_desc *desc) |
372e722e | 166 | { |
8c0fca81 | 167 | return desc - &gpio_desc[0]; |
372e722e | 168 | } |
79a9becd | 169 | EXPORT_SYMBOL_GPL(desc_to_gpio); |
372e722e AC |
170 | |
171 | ||
d2876d08 DB |
172 | /* Warn when drivers omit gpio_request() calls -- legal but ill-advised |
173 | * when setting direction, and otherwise illegal. Until board setup code | |
174 | * and drivers use explicit requests everywhere (which won't happen when | |
175 | * those calls have no teeth) we can't avoid autorequesting. This nag | |
35e8bb51 DB |
176 | * message should motivate switching to explicit requests... so should |
177 | * the weaker cleanup after faults, compared to gpio_request(). | |
8a0cecff DB |
178 | * |
179 | * NOTE: the autorequest mechanism is going away; at this point it's | |
180 | * only "legal" in the sense that (old) code using it won't break yet, | |
181 | * but instead only triggers a WARN() stack dump. | |
d2876d08 | 182 | */ |
372e722e | 183 | static int gpio_ensure_requested(struct gpio_desc *desc) |
d2876d08 | 184 | { |
8a0cecff | 185 | const struct gpio_chip *chip = desc->chip; |
372e722e | 186 | const int gpio = desc_to_gpio(desc); |
35e8bb51 | 187 | |
8a0cecff DB |
188 | if (WARN(test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0, |
189 | "autorequest GPIO-%d\n", gpio)) { | |
35e8bb51 | 190 | if (!try_module_get(chip->owner)) { |
7589e59f AS |
191 | gpiod_err(desc, "%s: module can't be gotten\n", |
192 | __func__); | |
35e8bb51 DB |
193 | clear_bit(FLAG_REQUESTED, &desc->flags); |
194 | /* lose */ | |
195 | return -EIO; | |
196 | } | |
d2876d08 | 197 | desc_set_label(desc, "[auto]"); |
35e8bb51 DB |
198 | /* caller must chip->request() w/o spinlock */ |
199 | if (chip->request) | |
200 | return 1; | |
d2876d08 | 201 | } |
35e8bb51 | 202 | return 0; |
d2876d08 DB |
203 | } |
204 | ||
79a9becd AC |
205 | /** |
206 | * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs | |
207 | * @desc: descriptor to return the chip of | |
208 | */ | |
209 | struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc) | |
372e722e | 210 | { |
bcabdef1 | 211 | return desc ? desc->chip : NULL; |
372e722e | 212 | } |
79a9becd | 213 | EXPORT_SYMBOL_GPL(gpiod_to_chip); |
d2876d08 | 214 | |
8d0aab2f AV |
215 | /* dynamic allocation of GPIOs, e.g. on a hotplugged device */ |
216 | static int gpiochip_find_base(int ngpio) | |
217 | { | |
83cabe33 AC |
218 | struct gpio_chip *chip; |
219 | int base = ARCH_NR_GPIOS - ngpio; | |
8d0aab2f | 220 | |
83cabe33 AC |
221 | list_for_each_entry_reverse(chip, &gpio_chips, list) { |
222 | /* found a free space? */ | |
223 | if (chip->base + chip->ngpio <= base) | |
224 | break; | |
225 | else | |
226 | /* nope, check the space right before the chip */ | |
227 | base = chip->base - ngpio; | |
8d0aab2f AV |
228 | } |
229 | ||
83cabe33 | 230 | if (gpio_is_valid(base)) { |
8d0aab2f | 231 | pr_debug("%s: found new base at %d\n", __func__, base); |
83cabe33 AC |
232 | return base; |
233 | } else { | |
234 | pr_err("%s: cannot find free range\n", __func__); | |
235 | return -ENOSPC; | |
169b6a7a | 236 | } |
169b6a7a AV |
237 | } |
238 | ||
79a9becd AC |
239 | /** |
240 | * gpiod_get_direction - return the current direction of a GPIO | |
241 | * @desc: GPIO to get the direction of | |
242 | * | |
243 | * Return GPIOF_DIR_IN or GPIOF_DIR_OUT, or an error code in case of error. | |
244 | * | |
245 | * This function may sleep if gpiod_cansleep() is true. | |
246 | */ | |
247 | int gpiod_get_direction(const struct gpio_desc *desc) | |
80b0a602 MN |
248 | { |
249 | struct gpio_chip *chip; | |
372e722e | 250 | unsigned offset; |
80b0a602 MN |
251 | int status = -EINVAL; |
252 | ||
372e722e AC |
253 | chip = gpiod_to_chip(desc); |
254 | offset = gpio_chip_hwgpio(desc); | |
80b0a602 MN |
255 | |
256 | if (!chip->get_direction) | |
257 | return status; | |
258 | ||
372e722e | 259 | status = chip->get_direction(chip, offset); |
80b0a602 MN |
260 | if (status > 0) { |
261 | /* GPIOF_DIR_IN, or other positive */ | |
262 | status = 1; | |
def63433 AC |
263 | /* FLAG_IS_OUT is just a cache of the result of get_direction(), |
264 | * so it does not affect constness per se */ | |
265 | clear_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); | |
80b0a602 MN |
266 | } |
267 | if (status == 0) { | |
268 | /* GPIOF_DIR_OUT */ | |
def63433 | 269 | set_bit(FLAG_IS_OUT, &((struct gpio_desc *)desc)->flags); |
80b0a602 MN |
270 | } |
271 | return status; | |
272 | } | |
79a9becd | 273 | EXPORT_SYMBOL_GPL(gpiod_get_direction); |
80b0a602 | 274 | |
d8f388d8 DB |
275 | #ifdef CONFIG_GPIO_SYSFS |
276 | ||
277 | /* lock protects against unexport_gpio() being called while | |
278 | * sysfs files are active. | |
279 | */ | |
280 | static DEFINE_MUTEX(sysfs_lock); | |
281 | ||
282 | /* | |
283 | * /sys/class/gpio/gpioN... only for GPIOs that are exported | |
284 | * /direction | |
285 | * * MAY BE OMITTED if kernel won't allow direction changes | |
286 | * * is read/write as "in" or "out" | |
287 | * * may also be written as "high" or "low", initializing | |
288 | * output value as specified ("out" implies "low") | |
289 | * /value | |
290 | * * always readable, subject to hardware behavior | |
291 | * * may be writable, as zero/nonzero | |
ff77c352 DG |
292 | * /edge |
293 | * * configures behavior of poll(2) on /value | |
294 | * * available only if pin can generate IRQs on input | |
295 | * * is read/write as "none", "falling", "rising", or "both" | |
07697461 JN |
296 | * /active_low |
297 | * * configures polarity of /value | |
298 | * * is read/write as zero/nonzero | |
299 | * * also affects existing and subsequent "falling" and "rising" | |
300 | * /edge configuration | |
d8f388d8 DB |
301 | */ |
302 | ||
303 | static ssize_t gpio_direction_show(struct device *dev, | |
304 | struct device_attribute *attr, char *buf) | |
305 | { | |
def63433 | 306 | const struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
307 | ssize_t status; |
308 | ||
309 | mutex_lock(&sysfs_lock); | |
310 | ||
476171ce | 311 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { |
d8f388d8 | 312 | status = -EIO; |
476171ce | 313 | } else { |
372e722e | 314 | gpiod_get_direction(desc); |
d8f388d8 DB |
315 | status = sprintf(buf, "%s\n", |
316 | test_bit(FLAG_IS_OUT, &desc->flags) | |
317 | ? "out" : "in"); | |
476171ce | 318 | } |
d8f388d8 DB |
319 | |
320 | mutex_unlock(&sysfs_lock); | |
321 | return status; | |
322 | } | |
323 | ||
324 | static ssize_t gpio_direction_store(struct device *dev, | |
325 | struct device_attribute *attr, const char *buf, size_t size) | |
326 | { | |
372e722e | 327 | struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
328 | ssize_t status; |
329 | ||
330 | mutex_lock(&sysfs_lock); | |
331 | ||
332 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
333 | status = -EIO; | |
334 | else if (sysfs_streq(buf, "high")) | |
372e722e | 335 | status = gpiod_direction_output(desc, 1); |
d8f388d8 | 336 | else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low")) |
372e722e | 337 | status = gpiod_direction_output(desc, 0); |
d8f388d8 | 338 | else if (sysfs_streq(buf, "in")) |
372e722e | 339 | status = gpiod_direction_input(desc); |
d8f388d8 DB |
340 | else |
341 | status = -EINVAL; | |
342 | ||
343 | mutex_unlock(&sysfs_lock); | |
344 | return status ? : size; | |
345 | } | |
346 | ||
07697461 | 347 | static /* const */ DEVICE_ATTR(direction, 0644, |
d8f388d8 DB |
348 | gpio_direction_show, gpio_direction_store); |
349 | ||
350 | static ssize_t gpio_value_show(struct device *dev, | |
351 | struct device_attribute *attr, char *buf) | |
352 | { | |
372e722e | 353 | struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
354 | ssize_t status; |
355 | ||
356 | mutex_lock(&sysfs_lock); | |
357 | ||
79a9becd | 358 | if (!test_bit(FLAG_EXPORT, &desc->flags)) |
d8f388d8 | 359 | status = -EIO; |
79a9becd AC |
360 | else |
361 | status = sprintf(buf, "%d\n", gpiod_get_value_cansleep(desc)); | |
d8f388d8 DB |
362 | |
363 | mutex_unlock(&sysfs_lock); | |
364 | return status; | |
365 | } | |
366 | ||
367 | static ssize_t gpio_value_store(struct device *dev, | |
368 | struct device_attribute *attr, const char *buf, size_t size) | |
369 | { | |
372e722e | 370 | struct gpio_desc *desc = dev_get_drvdata(dev); |
d8f388d8 DB |
371 | ssize_t status; |
372 | ||
373 | mutex_lock(&sysfs_lock); | |
374 | ||
375 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
376 | status = -EIO; | |
377 | else if (!test_bit(FLAG_IS_OUT, &desc->flags)) | |
378 | status = -EPERM; | |
379 | else { | |
380 | long value; | |
381 | ||
a3d88c92 | 382 | status = kstrtol(buf, 0, &value); |
d8f388d8 | 383 | if (status == 0) { |
79a9becd | 384 | gpiod_set_value_cansleep(desc, value); |
d8f388d8 DB |
385 | status = size; |
386 | } | |
387 | } | |
388 | ||
389 | mutex_unlock(&sysfs_lock); | |
390 | return status; | |
391 | } | |
392 | ||
07697461 | 393 | static const DEVICE_ATTR(value, 0644, |
d8f388d8 DB |
394 | gpio_value_show, gpio_value_store); |
395 | ||
ff77c352 DG |
396 | static irqreturn_t gpio_sysfs_irq(int irq, void *priv) |
397 | { | |
5ba1821d | 398 | struct sysfs_dirent *value_sd = priv; |
ff77c352 | 399 | |
5ba1821d | 400 | sysfs_notify_dirent(value_sd); |
ff77c352 DG |
401 | return IRQ_HANDLED; |
402 | } | |
403 | ||
ff77c352 DG |
404 | static int gpio_setup_irq(struct gpio_desc *desc, struct device *dev, |
405 | unsigned long gpio_flags) | |
406 | { | |
5ba1821d | 407 | struct sysfs_dirent *value_sd; |
ff77c352 DG |
408 | unsigned long irq_flags; |
409 | int ret, irq, id; | |
410 | ||
411 | if ((desc->flags & GPIO_TRIGGER_MASK) == gpio_flags) | |
412 | return 0; | |
413 | ||
372e722e | 414 | irq = gpiod_to_irq(desc); |
ff77c352 DG |
415 | if (irq < 0) |
416 | return -EIO; | |
417 | ||
5ba1821d DG |
418 | id = desc->flags >> ID_SHIFT; |
419 | value_sd = idr_find(&dirent_idr, id); | |
420 | if (value_sd) | |
421 | free_irq(irq, value_sd); | |
ff77c352 DG |
422 | |
423 | desc->flags &= ~GPIO_TRIGGER_MASK; | |
424 | ||
425 | if (!gpio_flags) { | |
d468bf9e | 426 | gpiod_unlock_as_irq(desc); |
ff77c352 | 427 | ret = 0; |
5ba1821d | 428 | goto free_id; |
ff77c352 DG |
429 | } |
430 | ||
431 | irq_flags = IRQF_SHARED; | |
432 | if (test_bit(FLAG_TRIG_FALL, &gpio_flags)) | |
07697461 JN |
433 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
434 | IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING; | |
ff77c352 | 435 | if (test_bit(FLAG_TRIG_RISE, &gpio_flags)) |
07697461 JN |
436 | irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ? |
437 | IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING; | |
ff77c352 | 438 | |
5ba1821d | 439 | if (!value_sd) { |
388975cc | 440 | value_sd = sysfs_get_dirent(dev->kobj.sd, "value"); |
5ba1821d DG |
441 | if (!value_sd) { |
442 | ret = -ENODEV; | |
ff77c352 DG |
443 | goto err_out; |
444 | } | |
445 | ||
62f516b8 TH |
446 | ret = idr_alloc(&dirent_idr, value_sd, 1, 0, GFP_KERNEL); |
447 | if (ret < 0) | |
5ba1821d | 448 | goto free_sd; |
62f516b8 | 449 | id = ret; |
ff77c352 DG |
450 | |
451 | desc->flags &= GPIO_FLAGS_MASK; | |
5ba1821d | 452 | desc->flags |= (unsigned long)id << ID_SHIFT; |
ff77c352 | 453 | |
5ba1821d | 454 | if (desc->flags >> ID_SHIFT != id) { |
ff77c352 DG |
455 | ret = -ERANGE; |
456 | goto free_id; | |
457 | } | |
ff77c352 DG |
458 | } |
459 | ||
364fadb3 | 460 | ret = request_any_context_irq(irq, gpio_sysfs_irq, irq_flags, |
5ba1821d | 461 | "gpiolib", value_sd); |
364fadb3 | 462 | if (ret < 0) |
5ba1821d | 463 | goto free_id; |
ff77c352 | 464 | |
d468bf9e LW |
465 | ret = gpiod_lock_as_irq(desc); |
466 | if (ret < 0) { | |
467 | gpiod_warn(desc, "failed to flag the GPIO for IRQ\n"); | |
468 | goto free_id; | |
469 | } | |
470 | ||
ff77c352 DG |
471 | desc->flags |= gpio_flags; |
472 | return 0; | |
473 | ||
ff77c352 | 474 | free_id: |
5ba1821d | 475 | idr_remove(&dirent_idr, id); |
ff77c352 | 476 | desc->flags &= GPIO_FLAGS_MASK; |
5ba1821d DG |
477 | free_sd: |
478 | if (value_sd) | |
479 | sysfs_put(value_sd); | |
ff77c352 DG |
480 | err_out: |
481 | return ret; | |
482 | } | |
483 | ||
484 | static const struct { | |
485 | const char *name; | |
486 | unsigned long flags; | |
487 | } trigger_types[] = { | |
488 | { "none", 0 }, | |
489 | { "falling", BIT(FLAG_TRIG_FALL) }, | |
490 | { "rising", BIT(FLAG_TRIG_RISE) }, | |
491 | { "both", BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE) }, | |
492 | }; | |
493 | ||
494 | static ssize_t gpio_edge_show(struct device *dev, | |
495 | struct device_attribute *attr, char *buf) | |
496 | { | |
497 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
498 | ssize_t status; | |
499 | ||
500 | mutex_lock(&sysfs_lock); | |
501 | ||
502 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
503 | status = -EIO; | |
504 | else { | |
505 | int i; | |
506 | ||
507 | status = 0; | |
508 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | |
509 | if ((desc->flags & GPIO_TRIGGER_MASK) | |
510 | == trigger_types[i].flags) { | |
511 | status = sprintf(buf, "%s\n", | |
512 | trigger_types[i].name); | |
513 | break; | |
514 | } | |
515 | } | |
516 | ||
517 | mutex_unlock(&sysfs_lock); | |
518 | return status; | |
519 | } | |
520 | ||
521 | static ssize_t gpio_edge_store(struct device *dev, | |
522 | struct device_attribute *attr, const char *buf, size_t size) | |
523 | { | |
524 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
525 | ssize_t status; | |
526 | int i; | |
527 | ||
528 | for (i = 0; i < ARRAY_SIZE(trigger_types); i++) | |
529 | if (sysfs_streq(trigger_types[i].name, buf)) | |
530 | goto found; | |
531 | return -EINVAL; | |
532 | ||
533 | found: | |
534 | mutex_lock(&sysfs_lock); | |
535 | ||
536 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
537 | status = -EIO; | |
538 | else { | |
539 | status = gpio_setup_irq(desc, dev, trigger_types[i].flags); | |
540 | if (!status) | |
541 | status = size; | |
542 | } | |
543 | ||
544 | mutex_unlock(&sysfs_lock); | |
545 | ||
546 | return status; | |
547 | } | |
548 | ||
549 | static DEVICE_ATTR(edge, 0644, gpio_edge_show, gpio_edge_store); | |
550 | ||
07697461 JN |
551 | static int sysfs_set_active_low(struct gpio_desc *desc, struct device *dev, |
552 | int value) | |
553 | { | |
554 | int status = 0; | |
555 | ||
556 | if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) | |
557 | return 0; | |
558 | ||
559 | if (value) | |
560 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
561 | else | |
562 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); | |
563 | ||
564 | /* reconfigure poll(2) support if enabled on one edge only */ | |
565 | if (dev != NULL && (!!test_bit(FLAG_TRIG_RISE, &desc->flags) ^ | |
566 | !!test_bit(FLAG_TRIG_FALL, &desc->flags))) { | |
567 | unsigned long trigger_flags = desc->flags & GPIO_TRIGGER_MASK; | |
568 | ||
569 | gpio_setup_irq(desc, dev, 0); | |
570 | status = gpio_setup_irq(desc, dev, trigger_flags); | |
571 | } | |
572 | ||
573 | return status; | |
574 | } | |
575 | ||
576 | static ssize_t gpio_active_low_show(struct device *dev, | |
577 | struct device_attribute *attr, char *buf) | |
578 | { | |
579 | const struct gpio_desc *desc = dev_get_drvdata(dev); | |
580 | ssize_t status; | |
581 | ||
582 | mutex_lock(&sysfs_lock); | |
583 | ||
584 | if (!test_bit(FLAG_EXPORT, &desc->flags)) | |
585 | status = -EIO; | |
586 | else | |
587 | status = sprintf(buf, "%d\n", | |
588 | !!test_bit(FLAG_ACTIVE_LOW, &desc->flags)); | |
589 | ||
590 | mutex_unlock(&sysfs_lock); | |
591 | ||
592 | return status; | |
593 | } | |
594 | ||
595 | static ssize_t gpio_active_low_store(struct device *dev, | |
596 | struct device_attribute *attr, const char *buf, size_t size) | |
597 | { | |
598 | struct gpio_desc *desc = dev_get_drvdata(dev); | |
599 | ssize_t status; | |
600 | ||
601 | mutex_lock(&sysfs_lock); | |
602 | ||
603 | if (!test_bit(FLAG_EXPORT, &desc->flags)) { | |
604 | status = -EIO; | |
605 | } else { | |
606 | long value; | |
607 | ||
a3d88c92 | 608 | status = kstrtol(buf, 0, &value); |
07697461 JN |
609 | if (status == 0) |
610 | status = sysfs_set_active_low(desc, dev, value != 0); | |
611 | } | |
612 | ||
613 | mutex_unlock(&sysfs_lock); | |
614 | ||
615 | return status ? : size; | |
616 | } | |
617 | ||
618 | static const DEVICE_ATTR(active_low, 0644, | |
619 | gpio_active_low_show, gpio_active_low_store); | |
620 | ||
d8f388d8 | 621 | static const struct attribute *gpio_attrs[] = { |
d8f388d8 | 622 | &dev_attr_value.attr, |
07697461 | 623 | &dev_attr_active_low.attr, |
d8f388d8 DB |
624 | NULL, |
625 | }; | |
626 | ||
627 | static const struct attribute_group gpio_attr_group = { | |
628 | .attrs = (struct attribute **) gpio_attrs, | |
629 | }; | |
630 | ||
631 | /* | |
632 | * /sys/class/gpio/gpiochipN/ | |
633 | * /base ... matching gpio_chip.base (N) | |
634 | * /label ... matching gpio_chip.label | |
635 | * /ngpio ... matching gpio_chip.ngpio | |
636 | */ | |
637 | ||
638 | static ssize_t chip_base_show(struct device *dev, | |
639 | struct device_attribute *attr, char *buf) | |
640 | { | |
641 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
642 | ||
643 | return sprintf(buf, "%d\n", chip->base); | |
644 | } | |
645 | static DEVICE_ATTR(base, 0444, chip_base_show, NULL); | |
646 | ||
647 | static ssize_t chip_label_show(struct device *dev, | |
648 | struct device_attribute *attr, char *buf) | |
649 | { | |
650 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
651 | ||
652 | return sprintf(buf, "%s\n", chip->label ? : ""); | |
653 | } | |
654 | static DEVICE_ATTR(label, 0444, chip_label_show, NULL); | |
655 | ||
656 | static ssize_t chip_ngpio_show(struct device *dev, | |
657 | struct device_attribute *attr, char *buf) | |
658 | { | |
659 | const struct gpio_chip *chip = dev_get_drvdata(dev); | |
660 | ||
661 | return sprintf(buf, "%u\n", chip->ngpio); | |
662 | } | |
663 | static DEVICE_ATTR(ngpio, 0444, chip_ngpio_show, NULL); | |
664 | ||
665 | static const struct attribute *gpiochip_attrs[] = { | |
666 | &dev_attr_base.attr, | |
667 | &dev_attr_label.attr, | |
668 | &dev_attr_ngpio.attr, | |
669 | NULL, | |
670 | }; | |
671 | ||
672 | static const struct attribute_group gpiochip_attr_group = { | |
673 | .attrs = (struct attribute **) gpiochip_attrs, | |
674 | }; | |
675 | ||
676 | /* | |
677 | * /sys/class/gpio/export ... write-only | |
678 | * integer N ... number of GPIO to export (full access) | |
679 | * /sys/class/gpio/unexport ... write-only | |
680 | * integer N ... number of GPIO to unexport | |
681 | */ | |
28812fe1 AK |
682 | static ssize_t export_store(struct class *class, |
683 | struct class_attribute *attr, | |
684 | const char *buf, size_t len) | |
d8f388d8 | 685 | { |
372e722e AC |
686 | long gpio; |
687 | struct gpio_desc *desc; | |
688 | int status; | |
d8f388d8 | 689 | |
a3d88c92 | 690 | status = kstrtol(buf, 0, &gpio); |
d8f388d8 DB |
691 | if (status < 0) |
692 | goto done; | |
693 | ||
372e722e | 694 | desc = gpio_to_desc(gpio); |
bcabdef1 AC |
695 | /* reject invalid GPIOs */ |
696 | if (!desc) { | |
697 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | |
698 | return -EINVAL; | |
699 | } | |
372e722e | 700 | |
d8f388d8 DB |
701 | /* No extra locking here; FLAG_SYSFS just signifies that the |
702 | * request and export were done by on behalf of userspace, so | |
703 | * they may be undone on its behalf too. | |
704 | */ | |
705 | ||
372e722e | 706 | status = gpiod_request(desc, "sysfs"); |
ad2fab36 MN |
707 | if (status < 0) { |
708 | if (status == -EPROBE_DEFER) | |
709 | status = -ENODEV; | |
d8f388d8 | 710 | goto done; |
ad2fab36 | 711 | } |
372e722e | 712 | status = gpiod_export(desc, true); |
d8f388d8 | 713 | if (status < 0) |
372e722e | 714 | gpiod_free(desc); |
d8f388d8 | 715 | else |
372e722e | 716 | set_bit(FLAG_SYSFS, &desc->flags); |
d8f388d8 DB |
717 | |
718 | done: | |
719 | if (status) | |
720 | pr_debug("%s: status %d\n", __func__, status); | |
721 | return status ? : len; | |
722 | } | |
723 | ||
28812fe1 AK |
724 | static ssize_t unexport_store(struct class *class, |
725 | struct class_attribute *attr, | |
726 | const char *buf, size_t len) | |
d8f388d8 | 727 | { |
372e722e AC |
728 | long gpio; |
729 | struct gpio_desc *desc; | |
730 | int status; | |
d8f388d8 | 731 | |
a3d88c92 | 732 | status = kstrtol(buf, 0, &gpio); |
d8f388d8 DB |
733 | if (status < 0) |
734 | goto done; | |
735 | ||
372e722e | 736 | desc = gpio_to_desc(gpio); |
d8f388d8 | 737 | /* reject bogus commands (gpio_unexport ignores them) */ |
bcabdef1 AC |
738 | if (!desc) { |
739 | pr_warn("%s: invalid GPIO %ld\n", __func__, gpio); | |
740 | return -EINVAL; | |
741 | } | |
742 | ||
743 | status = -EINVAL; | |
d8f388d8 DB |
744 | |
745 | /* No extra locking here; FLAG_SYSFS just signifies that the | |
746 | * request and export were done by on behalf of userspace, so | |
747 | * they may be undone on its behalf too. | |
748 | */ | |
372e722e | 749 | if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) { |
d8f388d8 | 750 | status = 0; |
372e722e | 751 | gpiod_free(desc); |
d8f388d8 DB |
752 | } |
753 | done: | |
754 | if (status) | |
755 | pr_debug("%s: status %d\n", __func__, status); | |
756 | return status ? : len; | |
757 | } | |
758 | ||
759 | static struct class_attribute gpio_class_attrs[] = { | |
760 | __ATTR(export, 0200, NULL, export_store), | |
761 | __ATTR(unexport, 0200, NULL, unexport_store), | |
762 | __ATTR_NULL, | |
763 | }; | |
764 | ||
765 | static struct class gpio_class = { | |
766 | .name = "gpio", | |
767 | .owner = THIS_MODULE, | |
768 | ||
769 | .class_attrs = gpio_class_attrs, | |
770 | }; | |
771 | ||
772 | ||
773 | /** | |
79a9becd | 774 | * gpiod_export - export a GPIO through sysfs |
d8f388d8 DB |
775 | * @gpio: gpio to make available, already requested |
776 | * @direction_may_change: true if userspace may change gpio direction | |
777 | * Context: arch_initcall or later | |
778 | * | |
779 | * When drivers want to make a GPIO accessible to userspace after they | |
780 | * have requested it -- perhaps while debugging, or as part of their | |
781 | * public interface -- they may use this routine. If the GPIO can | |
782 | * change direction (some can't) and the caller allows it, userspace | |
783 | * will see "direction" sysfs attribute which may be used to change | |
784 | * the gpio's direction. A "value" attribute will always be provided. | |
785 | * | |
786 | * Returns zero on success, else an error. | |
787 | */ | |
79a9becd | 788 | int gpiod_export(struct gpio_desc *desc, bool direction_may_change) |
d8f388d8 DB |
789 | { |
790 | unsigned long flags; | |
fc4e2514 | 791 | int status; |
62154991 | 792 | const char *ioname = NULL; |
fc4e2514 | 793 | struct device *dev; |
372e722e | 794 | int offset; |
d8f388d8 DB |
795 | |
796 | /* can't export until sysfs is available ... */ | |
797 | if (!gpio_class.p) { | |
798 | pr_debug("%s: called too early!\n", __func__); | |
799 | return -ENOENT; | |
800 | } | |
801 | ||
372e722e AC |
802 | if (!desc) { |
803 | pr_debug("%s: invalid gpio descriptor\n", __func__); | |
fc4e2514 RM |
804 | return -EINVAL; |
805 | } | |
d8f388d8 DB |
806 | |
807 | mutex_lock(&sysfs_lock); | |
808 | ||
809 | spin_lock_irqsave(&gpio_lock, flags); | |
fc4e2514 RM |
810 | if (!test_bit(FLAG_REQUESTED, &desc->flags) || |
811 | test_bit(FLAG_EXPORT, &desc->flags)) { | |
812 | spin_unlock_irqrestore(&gpio_lock, flags); | |
7589e59f AS |
813 | gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n", |
814 | __func__, | |
fc4e2514 RM |
815 | test_bit(FLAG_REQUESTED, &desc->flags), |
816 | test_bit(FLAG_EXPORT, &desc->flags)); | |
529f2ad5 DC |
817 | status = -EPERM; |
818 | goto fail_unlock; | |
d8f388d8 | 819 | } |
fc4e2514 RM |
820 | |
821 | if (!desc->chip->direction_input || !desc->chip->direction_output) | |
822 | direction_may_change = false; | |
d8f388d8 DB |
823 | spin_unlock_irqrestore(&gpio_lock, flags); |
824 | ||
372e722e AC |
825 | offset = gpio_chip_hwgpio(desc); |
826 | if (desc->chip->names && desc->chip->names[offset]) | |
827 | ioname = desc->chip->names[offset]; | |
926b663c | 828 | |
fc4e2514 | 829 | dev = device_create(&gpio_class, desc->chip->dev, MKDEV(0, 0), |
372e722e AC |
830 | desc, ioname ? ioname : "gpio%u", |
831 | desc_to_gpio(desc)); | |
fc4e2514 RM |
832 | if (IS_ERR(dev)) { |
833 | status = PTR_ERR(dev); | |
834 | goto fail_unlock; | |
d8f388d8 DB |
835 | } |
836 | ||
fc4e2514 | 837 | status = sysfs_create_group(&dev->kobj, &gpio_attr_group); |
d8f388d8 | 838 | if (status) |
fc4e2514 | 839 | goto fail_unregister_device; |
d8f388d8 | 840 | |
fc4e2514 RM |
841 | if (direction_may_change) { |
842 | status = device_create_file(dev, &dev_attr_direction); | |
843 | if (status) | |
844 | goto fail_unregister_device; | |
845 | } | |
846 | ||
372e722e | 847 | if (gpiod_to_irq(desc) >= 0 && (direction_may_change || |
fc4e2514 RM |
848 | !test_bit(FLAG_IS_OUT, &desc->flags))) { |
849 | status = device_create_file(dev, &dev_attr_edge); | |
850 | if (status) | |
851 | goto fail_unregister_device; | |
852 | } | |
d8f388d8 | 853 | |
fc4e2514 RM |
854 | set_bit(FLAG_EXPORT, &desc->flags); |
855 | mutex_unlock(&sysfs_lock); | |
856 | return 0; | |
857 | ||
858 | fail_unregister_device: | |
859 | device_unregister(dev); | |
860 | fail_unlock: | |
861 | mutex_unlock(&sysfs_lock); | |
7589e59f | 862 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
d8f388d8 DB |
863 | return status; |
864 | } | |
79a9becd | 865 | EXPORT_SYMBOL_GPL(gpiod_export); |
d8f388d8 | 866 | |
9f3b795a | 867 | static int match_export(struct device *dev, const void *data) |
d8f388d8 DB |
868 | { |
869 | return dev_get_drvdata(dev) == data; | |
870 | } | |
871 | ||
a4177ee7 | 872 | /** |
79a9becd | 873 | * gpiod_export_link - create a sysfs link to an exported GPIO node |
a4177ee7 JN |
874 | * @dev: device under which to create symlink |
875 | * @name: name of the symlink | |
876 | * @gpio: gpio to create symlink to, already exported | |
877 | * | |
878 | * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN | |
879 | * node. Caller is responsible for unlinking. | |
880 | * | |
881 | * Returns zero on success, else an error. | |
882 | */ | |
79a9becd AC |
883 | int gpiod_export_link(struct device *dev, const char *name, |
884 | struct gpio_desc *desc) | |
a4177ee7 | 885 | { |
a4177ee7 JN |
886 | int status = -EINVAL; |
887 | ||
bcabdef1 AC |
888 | if (!desc) { |
889 | pr_warn("%s: invalid GPIO\n", __func__); | |
890 | return -EINVAL; | |
891 | } | |
a4177ee7 JN |
892 | |
893 | mutex_lock(&sysfs_lock); | |
894 | ||
a4177ee7 JN |
895 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
896 | struct device *tdev; | |
897 | ||
898 | tdev = class_find_device(&gpio_class, NULL, desc, match_export); | |
899 | if (tdev != NULL) { | |
900 | status = sysfs_create_link(&dev->kobj, &tdev->kobj, | |
901 | name); | |
902 | } else { | |
903 | status = -ENODEV; | |
904 | } | |
905 | } | |
906 | ||
907 | mutex_unlock(&sysfs_lock); | |
908 | ||
a4177ee7 | 909 | if (status) |
7589e59f | 910 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
a4177ee7 JN |
911 | |
912 | return status; | |
913 | } | |
79a9becd | 914 | EXPORT_SYMBOL_GPL(gpiod_export_link); |
07697461 JN |
915 | |
916 | /** | |
79a9becd | 917 | * gpiod_sysfs_set_active_low - set the polarity of gpio sysfs value |
07697461 JN |
918 | * @gpio: gpio to change |
919 | * @value: non-zero to use active low, i.e. inverted values | |
920 | * | |
921 | * Set the polarity of /sys/class/gpio/gpioN/value sysfs attribute. | |
922 | * The GPIO does not have to be exported yet. If poll(2) support has | |
923 | * been enabled for either rising or falling edge, it will be | |
924 | * reconfigured to follow the new polarity. | |
925 | * | |
926 | * Returns zero on success, else an error. | |
927 | */ | |
79a9becd | 928 | int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value) |
07697461 | 929 | { |
07697461 JN |
930 | struct device *dev = NULL; |
931 | int status = -EINVAL; | |
932 | ||
bcabdef1 AC |
933 | if (!desc) { |
934 | pr_warn("%s: invalid GPIO\n", __func__); | |
935 | return -EINVAL; | |
936 | } | |
07697461 JN |
937 | |
938 | mutex_lock(&sysfs_lock); | |
939 | ||
07697461 | 940 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
07697461 JN |
941 | dev = class_find_device(&gpio_class, NULL, desc, match_export); |
942 | if (dev == NULL) { | |
943 | status = -ENODEV; | |
944 | goto unlock; | |
945 | } | |
946 | } | |
947 | ||
948 | status = sysfs_set_active_low(desc, dev, value); | |
949 | ||
950 | unlock: | |
951 | mutex_unlock(&sysfs_lock); | |
952 | ||
07697461 | 953 | if (status) |
7589e59f | 954 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
07697461 JN |
955 | |
956 | return status; | |
957 | } | |
79a9becd | 958 | EXPORT_SYMBOL_GPL(gpiod_sysfs_set_active_low); |
07697461 | 959 | |
d8f388d8 | 960 | /** |
79a9becd | 961 | * gpiod_unexport - reverse effect of gpio_export() |
d8f388d8 DB |
962 | * @gpio: gpio to make unavailable |
963 | * | |
964 | * This is implicit on gpio_free(). | |
965 | */ | |
79a9becd | 966 | void gpiod_unexport(struct gpio_desc *desc) |
d8f388d8 | 967 | { |
6a99ad4a | 968 | int status = 0; |
864533ce | 969 | struct device *dev = NULL; |
d8f388d8 | 970 | |
372e722e | 971 | if (!desc) { |
bcabdef1 AC |
972 | pr_warn("%s: invalid GPIO\n", __func__); |
973 | return; | |
6a99ad4a | 974 | } |
d8f388d8 DB |
975 | |
976 | mutex_lock(&sysfs_lock); | |
977 | ||
d8f388d8 | 978 | if (test_bit(FLAG_EXPORT, &desc->flags)) { |
d8f388d8 DB |
979 | |
980 | dev = class_find_device(&gpio_class, NULL, desc, match_export); | |
981 | if (dev) { | |
ff77c352 | 982 | gpio_setup_irq(desc, dev, 0); |
d8f388d8 | 983 | clear_bit(FLAG_EXPORT, &desc->flags); |
d8f388d8 DB |
984 | } else |
985 | status = -ENODEV; | |
986 | } | |
987 | ||
988 | mutex_unlock(&sysfs_lock); | |
372e722e | 989 | |
864533ce ML |
990 | if (dev) { |
991 | device_unregister(dev); | |
992 | put_device(dev); | |
993 | } | |
bcabdef1 | 994 | |
d8f388d8 | 995 | if (status) |
7589e59f | 996 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
372e722e | 997 | } |
79a9becd | 998 | EXPORT_SYMBOL_GPL(gpiod_unexport); |
d8f388d8 DB |
999 | |
1000 | static int gpiochip_export(struct gpio_chip *chip) | |
1001 | { | |
1002 | int status; | |
1003 | struct device *dev; | |
1004 | ||
1005 | /* Many systems register gpio chips for SOC support very early, | |
1006 | * before driver model support is available. In those cases we | |
1007 | * export this later, in gpiolib_sysfs_init() ... here we just | |
1008 | * verify that _some_ field of gpio_class got initialized. | |
1009 | */ | |
1010 | if (!gpio_class.p) | |
1011 | return 0; | |
1012 | ||
1013 | /* use chip->base for the ID; it's already known to be unique */ | |
1014 | mutex_lock(&sysfs_lock); | |
1015 | dev = device_create(&gpio_class, chip->dev, MKDEV(0, 0), chip, | |
1016 | "gpiochip%d", chip->base); | |
d62668e1 | 1017 | if (!IS_ERR(dev)) { |
d8f388d8 DB |
1018 | status = sysfs_create_group(&dev->kobj, |
1019 | &gpiochip_attr_group); | |
1020 | } else | |
d62668e1 | 1021 | status = PTR_ERR(dev); |
d8f388d8 DB |
1022 | chip->exported = (status == 0); |
1023 | mutex_unlock(&sysfs_lock); | |
1024 | ||
1025 | if (status) { | |
1026 | unsigned long flags; | |
1027 | unsigned gpio; | |
1028 | ||
1029 | spin_lock_irqsave(&gpio_lock, flags); | |
6c0b4e6c AC |
1030 | gpio = 0; |
1031 | while (gpio < chip->ngpio) | |
1032 | chip->desc[gpio++].chip = NULL; | |
d8f388d8 DB |
1033 | spin_unlock_irqrestore(&gpio_lock, flags); |
1034 | ||
1035 | pr_debug("%s: chip %s status %d\n", __func__, | |
1036 | chip->label, status); | |
1037 | } | |
1038 | ||
1039 | return status; | |
1040 | } | |
1041 | ||
1042 | static void gpiochip_unexport(struct gpio_chip *chip) | |
1043 | { | |
1044 | int status; | |
1045 | struct device *dev; | |
1046 | ||
1047 | mutex_lock(&sysfs_lock); | |
1048 | dev = class_find_device(&gpio_class, NULL, chip, match_export); | |
1049 | if (dev) { | |
1050 | put_device(dev); | |
1051 | device_unregister(dev); | |
9fb1f39e | 1052 | chip->exported = false; |
d8f388d8 DB |
1053 | status = 0; |
1054 | } else | |
1055 | status = -ENODEV; | |
1056 | mutex_unlock(&sysfs_lock); | |
1057 | ||
1058 | if (status) | |
1059 | pr_debug("%s: chip %s status %d\n", __func__, | |
1060 | chip->label, status); | |
1061 | } | |
1062 | ||
1063 | static int __init gpiolib_sysfs_init(void) | |
1064 | { | |
1065 | int status; | |
1066 | unsigned long flags; | |
65493e3a | 1067 | struct gpio_chip *chip; |
d8f388d8 DB |
1068 | |
1069 | status = class_register(&gpio_class); | |
1070 | if (status < 0) | |
1071 | return status; | |
1072 | ||
1073 | /* Scan and register the gpio_chips which registered very | |
1074 | * early (e.g. before the class_register above was called). | |
1075 | * | |
1076 | * We run before arch_initcall() so chip->dev nodes can have | |
1077 | * registered, and so arch_initcall() can always gpio_export(). | |
1078 | */ | |
1079 | spin_lock_irqsave(&gpio_lock, flags); | |
65493e3a | 1080 | list_for_each_entry(chip, &gpio_chips, list) { |
d8f388d8 DB |
1081 | if (!chip || chip->exported) |
1082 | continue; | |
1083 | ||
1084 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1085 | status = gpiochip_export(chip); | |
1086 | spin_lock_irqsave(&gpio_lock, flags); | |
1087 | } | |
1088 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1089 | ||
1090 | ||
1091 | return status; | |
1092 | } | |
1093 | postcore_initcall(gpiolib_sysfs_init); | |
1094 | ||
1095 | #else | |
1096 | static inline int gpiochip_export(struct gpio_chip *chip) | |
1097 | { | |
1098 | return 0; | |
1099 | } | |
1100 | ||
1101 | static inline void gpiochip_unexport(struct gpio_chip *chip) | |
1102 | { | |
1103 | } | |
1104 | ||
1105 | #endif /* CONFIG_GPIO_SYSFS */ | |
1106 | ||
1a989d0f AC |
1107 | /* |
1108 | * Add a new chip to the global chips list, keeping the list of chips sorted | |
1109 | * by base order. | |
1110 | * | |
1111 | * Return -EBUSY if the new chip overlaps with some other chip's integer | |
1112 | * space. | |
1113 | */ | |
1114 | static int gpiochip_add_to_list(struct gpio_chip *chip) | |
1115 | { | |
1116 | struct list_head *pos = &gpio_chips; | |
1117 | struct gpio_chip *_chip; | |
1118 | int err = 0; | |
1119 | ||
1120 | /* find where to insert our chip */ | |
1121 | list_for_each(pos, &gpio_chips) { | |
1122 | _chip = list_entry(pos, struct gpio_chip, list); | |
1123 | /* shall we insert before _chip? */ | |
1124 | if (_chip->base >= chip->base + chip->ngpio) | |
1125 | break; | |
1126 | } | |
1127 | ||
1128 | /* are we stepping on the chip right before? */ | |
1129 | if (pos != &gpio_chips && pos->prev != &gpio_chips) { | |
1130 | _chip = list_entry(pos->prev, struct gpio_chip, list); | |
1131 | if (_chip->base + _chip->ngpio > chip->base) { | |
1132 | dev_err(chip->dev, | |
1133 | "GPIO integer space overlap, cannot add chip\n"); | |
1134 | err = -EBUSY; | |
1135 | } | |
1136 | } | |
1137 | ||
1138 | if (!err) | |
1139 | list_add_tail(&chip->list, pos); | |
1140 | ||
1141 | return err; | |
1142 | } | |
1143 | ||
d2876d08 DB |
1144 | /** |
1145 | * gpiochip_add() - register a gpio_chip | |
1146 | * @chip: the chip to register, with chip->base initialized | |
1147 | * Context: potentially before irqs or kmalloc will work | |
1148 | * | |
1149 | * Returns a negative errno if the chip can't be registered, such as | |
1150 | * because the chip->base is invalid or already associated with a | |
1151 | * different chip. Otherwise it returns zero as a success code. | |
8d0aab2f | 1152 | * |
d8f388d8 DB |
1153 | * When gpiochip_add() is called very early during boot, so that GPIOs |
1154 | * can be freely used, the chip->dev device must be registered before | |
1155 | * the gpio framework's arch_initcall(). Otherwise sysfs initialization | |
1156 | * for GPIOs will fail rudely. | |
1157 | * | |
8d0aab2f AV |
1158 | * If chip->base is negative, this requests dynamic assignment of |
1159 | * a range of valid GPIOs. | |
d2876d08 DB |
1160 | */ |
1161 | int gpiochip_add(struct gpio_chip *chip) | |
1162 | { | |
1163 | unsigned long flags; | |
1164 | int status = 0; | |
1165 | unsigned id; | |
8d0aab2f | 1166 | int base = chip->base; |
d2876d08 | 1167 | |
bff5fda9 | 1168 | if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio - 1)) |
8d0aab2f | 1169 | && base >= 0) { |
d2876d08 DB |
1170 | status = -EINVAL; |
1171 | goto fail; | |
1172 | } | |
1173 | ||
1174 | spin_lock_irqsave(&gpio_lock, flags); | |
1175 | ||
8d0aab2f AV |
1176 | if (base < 0) { |
1177 | base = gpiochip_find_base(chip->ngpio); | |
1178 | if (base < 0) { | |
1179 | status = base; | |
d8f388d8 | 1180 | goto unlock; |
8d0aab2f AV |
1181 | } |
1182 | chip->base = base; | |
1183 | } | |
1184 | ||
1a989d0f AC |
1185 | status = gpiochip_add_to_list(chip); |
1186 | ||
d2876d08 | 1187 | if (status == 0) { |
6c0b4e6c AC |
1188 | chip->desc = &gpio_desc[chip->base]; |
1189 | ||
1190 | for (id = 0; id < chip->ngpio; id++) { | |
1191 | struct gpio_desc *desc = &chip->desc[id]; | |
1192 | desc->chip = chip; | |
d8f388d8 DB |
1193 | |
1194 | /* REVISIT: most hardware initializes GPIOs as | |
1195 | * inputs (often with pullups enabled) so power | |
1196 | * usage is minimized. Linux code should set the | |
1197 | * gpio direction first thing; but until it does, | |
80b0a602 | 1198 | * and in case chip->get_direction is not set, |
d8f388d8 DB |
1199 | * we may expose the wrong direction in sysfs. |
1200 | */ | |
6c0b4e6c | 1201 | desc->flags = !chip->direction_input |
d8f388d8 DB |
1202 | ? (1 << FLAG_IS_OUT) |
1203 | : 0; | |
d2876d08 DB |
1204 | } |
1205 | } | |
1206 | ||
3bae4811 ZG |
1207 | spin_unlock_irqrestore(&gpio_lock, flags); |
1208 | ||
f23f1516 SH |
1209 | #ifdef CONFIG_PINCTRL |
1210 | INIT_LIST_HEAD(&chip->pin_ranges); | |
1211 | #endif | |
1212 | ||
391c970c AV |
1213 | of_gpiochip_add(chip); |
1214 | ||
cedb1881 AV |
1215 | if (status) |
1216 | goto fail; | |
1217 | ||
1218 | status = gpiochip_export(chip); | |
1219 | if (status) | |
1220 | goto fail; | |
1221 | ||
7589e59f | 1222 | pr_debug("%s: registered GPIOs %d to %d on device: %s\n", __func__, |
64842aad GL |
1223 | chip->base, chip->base + chip->ngpio - 1, |
1224 | chip->label ? : "generic"); | |
1225 | ||
cedb1881 | 1226 | return 0; |
3bae4811 ZG |
1227 | |
1228 | unlock: | |
1229 | spin_unlock_irqrestore(&gpio_lock, flags); | |
d2876d08 DB |
1230 | fail: |
1231 | /* failures here can mean systems won't boot... */ | |
7589e59f | 1232 | pr_err("%s: GPIOs %d..%d (%s) failed to register\n", __func__, |
cedb1881 AV |
1233 | chip->base, chip->base + chip->ngpio - 1, |
1234 | chip->label ? : "generic"); | |
d2876d08 DB |
1235 | return status; |
1236 | } | |
1237 | EXPORT_SYMBOL_GPL(gpiochip_add); | |
1238 | ||
1239 | /** | |
1240 | * gpiochip_remove() - unregister a gpio_chip | |
1241 | * @chip: the chip to unregister | |
1242 | * | |
1243 | * A gpio_chip with any GPIOs still requested may not be removed. | |
1244 | */ | |
1245 | int gpiochip_remove(struct gpio_chip *chip) | |
1246 | { | |
1247 | unsigned long flags; | |
1248 | int status = 0; | |
1249 | unsigned id; | |
1250 | ||
1251 | spin_lock_irqsave(&gpio_lock, flags); | |
1252 | ||
9ef0d6f7 | 1253 | gpiochip_remove_pin_ranges(chip); |
391c970c AV |
1254 | of_gpiochip_remove(chip); |
1255 | ||
6c0b4e6c AC |
1256 | for (id = 0; id < chip->ngpio; id++) { |
1257 | if (test_bit(FLAG_REQUESTED, &chip->desc[id].flags)) { | |
d2876d08 DB |
1258 | status = -EBUSY; |
1259 | break; | |
1260 | } | |
1261 | } | |
1262 | if (status == 0) { | |
6c0b4e6c AC |
1263 | for (id = 0; id < chip->ngpio; id++) |
1264 | chip->desc[id].chip = NULL; | |
1a989d0f AC |
1265 | |
1266 | list_del(&chip->list); | |
d2876d08 DB |
1267 | } |
1268 | ||
1269 | spin_unlock_irqrestore(&gpio_lock, flags); | |
d8f388d8 DB |
1270 | |
1271 | if (status == 0) | |
1272 | gpiochip_unexport(chip); | |
1273 | ||
d2876d08 DB |
1274 | return status; |
1275 | } | |
1276 | EXPORT_SYMBOL_GPL(gpiochip_remove); | |
1277 | ||
594fa265 GL |
1278 | /** |
1279 | * gpiochip_find() - iterator for locating a specific gpio_chip | |
1280 | * @data: data to pass to match function | |
1281 | * @callback: Callback function to check gpio_chip | |
1282 | * | |
1283 | * Similar to bus_find_device. It returns a reference to a gpio_chip as | |
1284 | * determined by a user supplied @match callback. The callback should return | |
1285 | * 0 if the device doesn't match and non-zero if it does. If the callback is | |
1286 | * non-zero, this function will return to the caller and not iterate over any | |
1287 | * more gpio_chips. | |
1288 | */ | |
07ce8ec7 | 1289 | struct gpio_chip *gpiochip_find(void *data, |
6e2cf651 | 1290 | int (*match)(struct gpio_chip *chip, |
3d0f7cf0 | 1291 | void *data)) |
594fa265 | 1292 | { |
125eef96 | 1293 | struct gpio_chip *chip; |
594fa265 | 1294 | unsigned long flags; |
594fa265 GL |
1295 | |
1296 | spin_lock_irqsave(&gpio_lock, flags); | |
125eef96 AC |
1297 | list_for_each_entry(chip, &gpio_chips, list) |
1298 | if (match(chip, data)) | |
594fa265 | 1299 | break; |
125eef96 AC |
1300 | |
1301 | /* No match? */ | |
1302 | if (&chip->list == &gpio_chips) | |
1303 | chip = NULL; | |
594fa265 GL |
1304 | spin_unlock_irqrestore(&gpio_lock, flags); |
1305 | ||
1306 | return chip; | |
1307 | } | |
8fa0c9bf | 1308 | EXPORT_SYMBOL_GPL(gpiochip_find); |
d2876d08 | 1309 | |
79697ef9 AC |
1310 | static int gpiochip_match_name(struct gpio_chip *chip, void *data) |
1311 | { | |
1312 | const char *name = data; | |
1313 | ||
1314 | return !strcmp(chip->label, name); | |
1315 | } | |
1316 | ||
1317 | static struct gpio_chip *find_chip_by_name(const char *name) | |
1318 | { | |
1319 | return gpiochip_find((void *)name, gpiochip_match_name); | |
1320 | } | |
1321 | ||
f23f1516 | 1322 | #ifdef CONFIG_PINCTRL |
165adc9c | 1323 | |
586a87e6 CR |
1324 | /** |
1325 | * gpiochip_add_pingroup_range() - add a range for GPIO <-> pin mapping | |
1326 | * @chip: the gpiochip to add the range for | |
1327 | * @pinctrl: the dev_name() of the pin controller to map to | |
1328 | * @gpio_offset: the start offset in the current gpio_chip number space | |
1329 | * @pin_group: name of the pin group inside the pin controller | |
1330 | */ | |
1331 | int gpiochip_add_pingroup_range(struct gpio_chip *chip, | |
1332 | struct pinctrl_dev *pctldev, | |
1333 | unsigned int gpio_offset, const char *pin_group) | |
1334 | { | |
1335 | struct gpio_pin_range *pin_range; | |
1336 | int ret; | |
1337 | ||
1338 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); | |
1339 | if (!pin_range) { | |
1340 | pr_err("%s: GPIO chip: failed to allocate pin ranges\n", | |
1341 | chip->label); | |
1342 | return -ENOMEM; | |
1343 | } | |
1344 | ||
1345 | /* Use local offset as range ID */ | |
1346 | pin_range->range.id = gpio_offset; | |
1347 | pin_range->range.gc = chip; | |
1348 | pin_range->range.name = chip->label; | |
1349 | pin_range->range.base = chip->base + gpio_offset; | |
1350 | pin_range->pctldev = pctldev; | |
1351 | ||
1352 | ret = pinctrl_get_group_pins(pctldev, pin_group, | |
1353 | &pin_range->range.pins, | |
1354 | &pin_range->range.npins); | |
61c6375d MN |
1355 | if (ret < 0) { |
1356 | kfree(pin_range); | |
586a87e6 | 1357 | return ret; |
61c6375d | 1358 | } |
586a87e6 CR |
1359 | |
1360 | pinctrl_add_gpio_range(pctldev, &pin_range->range); | |
1361 | ||
1362 | pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PINGRP %s\n", | |
1363 | chip->label, gpio_offset, | |
1364 | gpio_offset + pin_range->range.npins - 1, | |
1365 | pinctrl_dev_get_devname(pctldev), pin_group); | |
1366 | ||
1367 | list_add_tail(&pin_range->node, &chip->pin_ranges); | |
1368 | ||
1369 | return 0; | |
1370 | } | |
1371 | EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range); | |
1372 | ||
3f0f8670 LW |
1373 | /** |
1374 | * gpiochip_add_pin_range() - add a range for GPIO <-> pin mapping | |
1375 | * @chip: the gpiochip to add the range for | |
1376 | * @pinctrl_name: the dev_name() of the pin controller to map to | |
316511c0 LW |
1377 | * @gpio_offset: the start offset in the current gpio_chip number space |
1378 | * @pin_offset: the start offset in the pin controller number space | |
3f0f8670 LW |
1379 | * @npins: the number of pins from the offset of each pin space (GPIO and |
1380 | * pin controller) to accumulate in this range | |
1381 | */ | |
1e63d7b9 | 1382 | int gpiochip_add_pin_range(struct gpio_chip *chip, const char *pinctl_name, |
316511c0 | 1383 | unsigned int gpio_offset, unsigned int pin_offset, |
3f0f8670 | 1384 | unsigned int npins) |
f23f1516 SH |
1385 | { |
1386 | struct gpio_pin_range *pin_range; | |
b4d4b1f0 | 1387 | int ret; |
f23f1516 | 1388 | |
3f0f8670 | 1389 | pin_range = kzalloc(sizeof(*pin_range), GFP_KERNEL); |
f23f1516 SH |
1390 | if (!pin_range) { |
1391 | pr_err("%s: GPIO chip: failed to allocate pin ranges\n", | |
1392 | chip->label); | |
1e63d7b9 | 1393 | return -ENOMEM; |
f23f1516 SH |
1394 | } |
1395 | ||
3f0f8670 | 1396 | /* Use local offset as range ID */ |
316511c0 | 1397 | pin_range->range.id = gpio_offset; |
3f0f8670 | 1398 | pin_range->range.gc = chip; |
f23f1516 | 1399 | pin_range->range.name = chip->label; |
316511c0 LW |
1400 | pin_range->range.base = chip->base + gpio_offset; |
1401 | pin_range->range.pin_base = pin_offset; | |
f23f1516 | 1402 | pin_range->range.npins = npins; |
192c369c | 1403 | pin_range->pctldev = pinctrl_find_and_add_gpio_range(pinctl_name, |
f23f1516 | 1404 | &pin_range->range); |
8f23ca1a | 1405 | if (IS_ERR(pin_range->pctldev)) { |
b4d4b1f0 | 1406 | ret = PTR_ERR(pin_range->pctldev); |
3f0f8670 LW |
1407 | pr_err("%s: GPIO chip: could not create pin range\n", |
1408 | chip->label); | |
1409 | kfree(pin_range); | |
b4d4b1f0 | 1410 | return ret; |
3f0f8670 | 1411 | } |
316511c0 LW |
1412 | pr_debug("GPIO chip %s: created GPIO range %d->%d ==> %s PIN %d->%d\n", |
1413 | chip->label, gpio_offset, gpio_offset + npins - 1, | |
1414 | pinctl_name, | |
1415 | pin_offset, pin_offset + npins - 1); | |
f23f1516 SH |
1416 | |
1417 | list_add_tail(&pin_range->node, &chip->pin_ranges); | |
1e63d7b9 LW |
1418 | |
1419 | return 0; | |
f23f1516 | 1420 | } |
165adc9c | 1421 | EXPORT_SYMBOL_GPL(gpiochip_add_pin_range); |
f23f1516 | 1422 | |
3f0f8670 LW |
1423 | /** |
1424 | * gpiochip_remove_pin_ranges() - remove all the GPIO <-> pin mappings | |
1425 | * @chip: the chip to remove all the mappings for | |
1426 | */ | |
f23f1516 SH |
1427 | void gpiochip_remove_pin_ranges(struct gpio_chip *chip) |
1428 | { | |
1429 | struct gpio_pin_range *pin_range, *tmp; | |
1430 | ||
1431 | list_for_each_entry_safe(pin_range, tmp, &chip->pin_ranges, node) { | |
1432 | list_del(&pin_range->node); | |
1433 | pinctrl_remove_gpio_range(pin_range->pctldev, | |
1434 | &pin_range->range); | |
3f0f8670 | 1435 | kfree(pin_range); |
f23f1516 SH |
1436 | } |
1437 | } | |
165adc9c LW |
1438 | EXPORT_SYMBOL_GPL(gpiochip_remove_pin_ranges); |
1439 | ||
1440 | #endif /* CONFIG_PINCTRL */ | |
f23f1516 | 1441 | |
d2876d08 DB |
1442 | /* These "optional" allocation calls help prevent drivers from stomping |
1443 | * on each other, and help provide better diagnostics in debugfs. | |
1444 | * They're called even less than the "set direction" calls. | |
1445 | */ | |
372e722e | 1446 | static int gpiod_request(struct gpio_desc *desc, const char *label) |
d2876d08 | 1447 | { |
35e8bb51 | 1448 | struct gpio_chip *chip; |
e9354576 | 1449 | int status = -EPROBE_DEFER; |
d2876d08 DB |
1450 | unsigned long flags; |
1451 | ||
0204df47 | 1452 | if (!desc) { |
bcabdef1 AC |
1453 | pr_warn("%s: invalid GPIO\n", __func__); |
1454 | return -EINVAL; | |
ad2fab36 | 1455 | } |
bcabdef1 AC |
1456 | |
1457 | spin_lock_irqsave(&gpio_lock, flags); | |
1458 | ||
35e8bb51 | 1459 | chip = desc->chip; |
0204df47 AC |
1460 | if (chip == NULL) |
1461 | goto done; | |
d2876d08 | 1462 | |
35e8bb51 | 1463 | if (!try_module_get(chip->owner)) |
438d8908 GL |
1464 | goto done; |
1465 | ||
d2876d08 | 1466 | /* NOTE: gpio_request() can be called in early boot, |
35e8bb51 | 1467 | * before IRQs are enabled, for non-sleeping (SOC) GPIOs. |
d2876d08 DB |
1468 | */ |
1469 | ||
1470 | if (test_and_set_bit(FLAG_REQUESTED, &desc->flags) == 0) { | |
1471 | desc_set_label(desc, label ? : "?"); | |
1472 | status = 0; | |
438d8908 | 1473 | } else { |
d2876d08 | 1474 | status = -EBUSY; |
35e8bb51 | 1475 | module_put(chip->owner); |
7460db56 | 1476 | goto done; |
35e8bb51 DB |
1477 | } |
1478 | ||
1479 | if (chip->request) { | |
1480 | /* chip->request may sleep */ | |
1481 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 1482 | status = chip->request(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
1483 | spin_lock_irqsave(&gpio_lock, flags); |
1484 | ||
1485 | if (status < 0) { | |
1486 | desc_set_label(desc, NULL); | |
1487 | module_put(chip->owner); | |
1488 | clear_bit(FLAG_REQUESTED, &desc->flags); | |
80b0a602 | 1489 | goto done; |
35e8bb51 | 1490 | } |
438d8908 | 1491 | } |
80b0a602 MN |
1492 | if (chip->get_direction) { |
1493 | /* chip->get_direction may sleep */ | |
1494 | spin_unlock_irqrestore(&gpio_lock, flags); | |
372e722e | 1495 | gpiod_get_direction(desc); |
80b0a602 MN |
1496 | spin_lock_irqsave(&gpio_lock, flags); |
1497 | } | |
d2876d08 DB |
1498 | done: |
1499 | if (status) | |
7589e59f | 1500 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
d2876d08 DB |
1501 | spin_unlock_irqrestore(&gpio_lock, flags); |
1502 | return status; | |
1503 | } | |
372e722e AC |
1504 | |
1505 | int gpio_request(unsigned gpio, const char *label) | |
1506 | { | |
1507 | return gpiod_request(gpio_to_desc(gpio), label); | |
1508 | } | |
d2876d08 DB |
1509 | EXPORT_SYMBOL_GPL(gpio_request); |
1510 | ||
372e722e | 1511 | static void gpiod_free(struct gpio_desc *desc) |
d2876d08 DB |
1512 | { |
1513 | unsigned long flags; | |
35e8bb51 | 1514 | struct gpio_chip *chip; |
d2876d08 | 1515 | |
3d599d1c UKK |
1516 | might_sleep(); |
1517 | ||
372e722e | 1518 | if (!desc) { |
d2876d08 DB |
1519 | WARN_ON(extra_checks); |
1520 | return; | |
1521 | } | |
1522 | ||
372e722e | 1523 | gpiod_unexport(desc); |
d8f388d8 | 1524 | |
d2876d08 DB |
1525 | spin_lock_irqsave(&gpio_lock, flags); |
1526 | ||
35e8bb51 DB |
1527 | chip = desc->chip; |
1528 | if (chip && test_bit(FLAG_REQUESTED, &desc->flags)) { | |
1529 | if (chip->free) { | |
1530 | spin_unlock_irqrestore(&gpio_lock, flags); | |
9c4ba946 | 1531 | might_sleep_if(chip->can_sleep); |
372e722e | 1532 | chip->free(chip, gpio_chip_hwgpio(desc)); |
35e8bb51 DB |
1533 | spin_lock_irqsave(&gpio_lock, flags); |
1534 | } | |
d2876d08 | 1535 | desc_set_label(desc, NULL); |
438d8908 | 1536 | module_put(desc->chip->owner); |
07697461 | 1537 | clear_bit(FLAG_ACTIVE_LOW, &desc->flags); |
35e8bb51 | 1538 | clear_bit(FLAG_REQUESTED, &desc->flags); |
aca5ce14 | 1539 | clear_bit(FLAG_OPEN_DRAIN, &desc->flags); |
25553ff0 | 1540 | clear_bit(FLAG_OPEN_SOURCE, &desc->flags); |
438d8908 | 1541 | } else |
d2876d08 DB |
1542 | WARN_ON(extra_checks); |
1543 | ||
1544 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1545 | } | |
372e722e AC |
1546 | |
1547 | void gpio_free(unsigned gpio) | |
1548 | { | |
1549 | gpiod_free(gpio_to_desc(gpio)); | |
1550 | } | |
d2876d08 DB |
1551 | EXPORT_SYMBOL_GPL(gpio_free); |
1552 | ||
3e45f1d1 EM |
1553 | /** |
1554 | * gpio_request_one - request a single GPIO with initial configuration | |
1555 | * @gpio: the GPIO number | |
1556 | * @flags: GPIO configuration as specified by GPIOF_* | |
1557 | * @label: a literal description string of this GPIO | |
1558 | */ | |
1559 | int gpio_request_one(unsigned gpio, unsigned long flags, const char *label) | |
1560 | { | |
372e722e | 1561 | struct gpio_desc *desc; |
3e45f1d1 EM |
1562 | int err; |
1563 | ||
372e722e AC |
1564 | desc = gpio_to_desc(gpio); |
1565 | ||
1566 | err = gpiod_request(desc, label); | |
3e45f1d1 EM |
1567 | if (err) |
1568 | return err; | |
1569 | ||
aca5ce14 | 1570 | if (flags & GPIOF_OPEN_DRAIN) |
372e722e | 1571 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); |
aca5ce14 | 1572 | |
25553ff0 | 1573 | if (flags & GPIOF_OPEN_SOURCE) |
372e722e | 1574 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); |
25553ff0 | 1575 | |
3e45f1d1 | 1576 | if (flags & GPIOF_DIR_IN) |
372e722e | 1577 | err = gpiod_direction_input(desc); |
3e45f1d1 | 1578 | else |
372e722e | 1579 | err = gpiod_direction_output(desc, |
3e45f1d1 EM |
1580 | (flags & GPIOF_INIT_HIGH) ? 1 : 0); |
1581 | ||
e254811c | 1582 | if (err) |
fc3a1f04 WS |
1583 | goto free_gpio; |
1584 | ||
1585 | if (flags & GPIOF_EXPORT) { | |
372e722e | 1586 | err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE); |
fc3a1f04 WS |
1587 | if (err) |
1588 | goto free_gpio; | |
1589 | } | |
1590 | ||
1591 | return 0; | |
e254811c | 1592 | |
fc3a1f04 | 1593 | free_gpio: |
372e722e | 1594 | gpiod_free(desc); |
3e45f1d1 EM |
1595 | return err; |
1596 | } | |
1597 | EXPORT_SYMBOL_GPL(gpio_request_one); | |
1598 | ||
1599 | /** | |
1600 | * gpio_request_array - request multiple GPIOs in a single call | |
1601 | * @array: array of the 'struct gpio' | |
1602 | * @num: how many GPIOs in the array | |
1603 | */ | |
7c295975 | 1604 | int gpio_request_array(const struct gpio *array, size_t num) |
3e45f1d1 EM |
1605 | { |
1606 | int i, err; | |
1607 | ||
1608 | for (i = 0; i < num; i++, array++) { | |
1609 | err = gpio_request_one(array->gpio, array->flags, array->label); | |
1610 | if (err) | |
1611 | goto err_free; | |
1612 | } | |
1613 | return 0; | |
1614 | ||
1615 | err_free: | |
1616 | while (i--) | |
1617 | gpio_free((--array)->gpio); | |
1618 | return err; | |
1619 | } | |
1620 | EXPORT_SYMBOL_GPL(gpio_request_array); | |
1621 | ||
1622 | /** | |
1623 | * gpio_free_array - release multiple GPIOs in a single call | |
1624 | * @array: array of the 'struct gpio' | |
1625 | * @num: how many GPIOs in the array | |
1626 | */ | |
7c295975 | 1627 | void gpio_free_array(const struct gpio *array, size_t num) |
3e45f1d1 EM |
1628 | { |
1629 | while (num--) | |
1630 | gpio_free((array++)->gpio); | |
1631 | } | |
1632 | EXPORT_SYMBOL_GPL(gpio_free_array); | |
d2876d08 DB |
1633 | |
1634 | /** | |
1635 | * gpiochip_is_requested - return string iff signal was requested | |
1636 | * @chip: controller managing the signal | |
1637 | * @offset: of signal within controller's 0..(ngpio - 1) range | |
1638 | * | |
1639 | * Returns NULL if the GPIO is not currently requested, else a string. | |
1640 | * If debugfs support is enabled, the string returned is the label passed | |
1641 | * to gpio_request(); otherwise it is a meaningless constant. | |
1642 | * | |
1643 | * This function is for use by GPIO controller drivers. The label can | |
1644 | * help with diagnostics, and knowing that the signal is used as a GPIO | |
1645 | * can help avoid accidentally multiplexing it to another controller. | |
1646 | */ | |
1647 | const char *gpiochip_is_requested(struct gpio_chip *chip, unsigned offset) | |
1648 | { | |
6c0b4e6c | 1649 | struct gpio_desc *desc; |
d2876d08 | 1650 | |
6c0b4e6c | 1651 | if (!GPIO_OFFSET_VALID(chip, offset)) |
d2876d08 | 1652 | return NULL; |
6c0b4e6c AC |
1653 | |
1654 | desc = &chip->desc[offset]; | |
1655 | ||
372e722e | 1656 | if (test_bit(FLAG_REQUESTED, &desc->flags) == 0) |
d2876d08 DB |
1657 | return NULL; |
1658 | #ifdef CONFIG_DEBUG_FS | |
372e722e | 1659 | return desc->label; |
d2876d08 DB |
1660 | #else |
1661 | return "?"; | |
1662 | #endif | |
1663 | } | |
1664 | EXPORT_SYMBOL_GPL(gpiochip_is_requested); | |
1665 | ||
1666 | ||
1667 | /* Drivers MUST set GPIO direction before making get/set calls. In | |
1668 | * some cases this is done in early boot, before IRQs are enabled. | |
1669 | * | |
1670 | * As a rule these aren't called more than once (except for drivers | |
1671 | * using the open-drain emulation idiom) so these are natural places | |
1672 | * to accumulate extra debugging checks. Note that we can't (yet) | |
1673 | * rely on gpio_request() having been called beforehand. | |
1674 | */ | |
1675 | ||
79a9becd AC |
1676 | /** |
1677 | * gpiod_direction_input - set the GPIO direction to input | |
1678 | * @desc: GPIO to set to input | |
1679 | * | |
1680 | * Set the direction of the passed GPIO to input, such as gpiod_get_value() can | |
1681 | * be called safely on it. | |
1682 | * | |
1683 | * Return 0 in case of success, else an error code. | |
1684 | */ | |
1685 | int gpiod_direction_input(struct gpio_desc *desc) | |
d2876d08 DB |
1686 | { |
1687 | unsigned long flags; | |
1688 | struct gpio_chip *chip; | |
d2876d08 | 1689 | int status = -EINVAL; |
372e722e | 1690 | int offset; |
d2876d08 | 1691 | |
be1a4b13 | 1692 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1693 | pr_warn("%s: invalid GPIO\n", __func__); |
1694 | return -EINVAL; | |
1695 | } | |
1696 | ||
be1a4b13 LW |
1697 | chip = desc->chip; |
1698 | if (!chip->get || !chip->direction_input) { | |
6424de5a MB |
1699 | gpiod_warn(desc, |
1700 | "%s: missing get() or direction_input() operations\n", | |
7589e59f | 1701 | __func__); |
be1a4b13 LW |
1702 | return -EIO; |
1703 | } | |
1704 | ||
d2876d08 DB |
1705 | spin_lock_irqsave(&gpio_lock, flags); |
1706 | ||
372e722e | 1707 | status = gpio_ensure_requested(desc); |
35e8bb51 DB |
1708 | if (status < 0) |
1709 | goto fail; | |
d2876d08 DB |
1710 | |
1711 | /* now we know the gpio is valid and chip won't vanish */ | |
1712 | ||
1713 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1714 | ||
9c4ba946 | 1715 | might_sleep_if(chip->can_sleep); |
d2876d08 | 1716 | |
372e722e | 1717 | offset = gpio_chip_hwgpio(desc); |
35e8bb51 | 1718 | if (status) { |
372e722e | 1719 | status = chip->request(chip, offset); |
35e8bb51 | 1720 | if (status < 0) { |
7589e59f AS |
1721 | gpiod_dbg(desc, "%s: chip request fail, %d\n", |
1722 | __func__, status); | |
35e8bb51 DB |
1723 | /* and it's not available to anyone else ... |
1724 | * gpio_request() is the fully clean solution. | |
1725 | */ | |
1726 | goto lose; | |
1727 | } | |
1728 | } | |
1729 | ||
372e722e | 1730 | status = chip->direction_input(chip, offset); |
d2876d08 DB |
1731 | if (status == 0) |
1732 | clear_bit(FLAG_IS_OUT, &desc->flags); | |
3f397c21 | 1733 | |
372e722e | 1734 | trace_gpio_direction(desc_to_gpio(desc), 1, status); |
35e8bb51 | 1735 | lose: |
d2876d08 DB |
1736 | return status; |
1737 | fail: | |
1738 | spin_unlock_irqrestore(&gpio_lock, flags); | |
bcabdef1 | 1739 | if (status) |
7589e59f | 1740 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
d2876d08 DB |
1741 | return status; |
1742 | } | |
79a9becd | 1743 | EXPORT_SYMBOL_GPL(gpiod_direction_input); |
372e722e | 1744 | |
79a9becd AC |
1745 | /** |
1746 | * gpiod_direction_output - set the GPIO direction to input | |
1747 | * @desc: GPIO to set to output | |
1748 | * @value: initial output value of the GPIO | |
1749 | * | |
1750 | * Set the direction of the passed GPIO to output, such as gpiod_set_value() can | |
1751 | * be called safely on it. The initial value of the output must be specified. | |
1752 | * | |
1753 | * Return 0 in case of success, else an error code. | |
1754 | */ | |
1755 | int gpiod_direction_output(struct gpio_desc *desc, int value) | |
d2876d08 DB |
1756 | { |
1757 | unsigned long flags; | |
1758 | struct gpio_chip *chip; | |
d2876d08 | 1759 | int status = -EINVAL; |
372e722e | 1760 | int offset; |
d2876d08 | 1761 | |
be1a4b13 | 1762 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1763 | pr_warn("%s: invalid GPIO\n", __func__); |
1764 | return -EINVAL; | |
1765 | } | |
1766 | ||
d468bf9e LW |
1767 | /* GPIOs used for IRQs shall not be set as output */ |
1768 | if (test_bit(FLAG_USED_AS_IRQ, &desc->flags)) { | |
1769 | gpiod_err(desc, | |
1770 | "%s: tried to set a GPIO tied to an IRQ as output\n", | |
1771 | __func__); | |
1772 | return -EIO; | |
1773 | } | |
1774 | ||
aca5ce14 LD |
1775 | /* Open drain pin should not be driven to 1 */ |
1776 | if (value && test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
372e722e | 1777 | return gpiod_direction_input(desc); |
aca5ce14 | 1778 | |
25553ff0 LD |
1779 | /* Open source pin should not be driven to 0 */ |
1780 | if (!value && test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
372e722e | 1781 | return gpiod_direction_input(desc); |
25553ff0 | 1782 | |
be1a4b13 LW |
1783 | chip = desc->chip; |
1784 | if (!chip->set || !chip->direction_output) { | |
6424de5a MB |
1785 | gpiod_warn(desc, |
1786 | "%s: missing set() or direction_output() operations\n", | |
1787 | __func__); | |
be1a4b13 LW |
1788 | return -EIO; |
1789 | } | |
1790 | ||
d2876d08 DB |
1791 | spin_lock_irqsave(&gpio_lock, flags); |
1792 | ||
372e722e | 1793 | status = gpio_ensure_requested(desc); |
35e8bb51 DB |
1794 | if (status < 0) |
1795 | goto fail; | |
d2876d08 DB |
1796 | |
1797 | /* now we know the gpio is valid and chip won't vanish */ | |
1798 | ||
1799 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1800 | ||
9c4ba946 | 1801 | might_sleep_if(chip->can_sleep); |
d2876d08 | 1802 | |
372e722e | 1803 | offset = gpio_chip_hwgpio(desc); |
35e8bb51 | 1804 | if (status) { |
372e722e | 1805 | status = chip->request(chip, offset); |
35e8bb51 | 1806 | if (status < 0) { |
7589e59f AS |
1807 | gpiod_dbg(desc, "%s: chip request fail, %d\n", |
1808 | __func__, status); | |
35e8bb51 DB |
1809 | /* and it's not available to anyone else ... |
1810 | * gpio_request() is the fully clean solution. | |
1811 | */ | |
1812 | goto lose; | |
1813 | } | |
1814 | } | |
1815 | ||
372e722e | 1816 | status = chip->direction_output(chip, offset, value); |
d2876d08 DB |
1817 | if (status == 0) |
1818 | set_bit(FLAG_IS_OUT, &desc->flags); | |
372e722e AC |
1819 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
1820 | trace_gpio_direction(desc_to_gpio(desc), 0, status); | |
35e8bb51 | 1821 | lose: |
d2876d08 DB |
1822 | return status; |
1823 | fail: | |
1824 | spin_unlock_irqrestore(&gpio_lock, flags); | |
bcabdef1 | 1825 | if (status) |
6424de5a | 1826 | gpiod_dbg(desc, "%s: gpio status %d\n", __func__, status); |
d2876d08 DB |
1827 | return status; |
1828 | } | |
79a9becd | 1829 | EXPORT_SYMBOL_GPL(gpiod_direction_output); |
d2876d08 | 1830 | |
c4b5be98 | 1831 | /** |
79a9becd | 1832 | * gpiod_set_debounce - sets @debounce time for a @gpio |
c4b5be98 FB |
1833 | * @gpio: the gpio to set debounce time |
1834 | * @debounce: debounce time is microseconds | |
65d87656 LW |
1835 | * |
1836 | * returns -ENOTSUPP if the controller does not support setting | |
1837 | * debounce. | |
c4b5be98 | 1838 | */ |
79a9becd | 1839 | int gpiod_set_debounce(struct gpio_desc *desc, unsigned debounce) |
c4b5be98 FB |
1840 | { |
1841 | unsigned long flags; | |
1842 | struct gpio_chip *chip; | |
c4b5be98 | 1843 | int status = -EINVAL; |
372e722e | 1844 | int offset; |
c4b5be98 | 1845 | |
be1a4b13 | 1846 | if (!desc || !desc->chip) { |
bcabdef1 AC |
1847 | pr_warn("%s: invalid GPIO\n", __func__); |
1848 | return -EINVAL; | |
1849 | } | |
1850 | ||
c4b5be98 | 1851 | chip = desc->chip; |
be1a4b13 | 1852 | if (!chip->set || !chip->set_debounce) { |
6424de5a MB |
1853 | gpiod_dbg(desc, |
1854 | "%s: missing set() or set_debounce() operations\n", | |
1855 | __func__); | |
65d87656 | 1856 | return -ENOTSUPP; |
be1a4b13 LW |
1857 | } |
1858 | ||
1859 | spin_lock_irqsave(&gpio_lock, flags); | |
372e722e AC |
1860 | |
1861 | status = gpio_ensure_requested(desc); | |
c4b5be98 FB |
1862 | if (status < 0) |
1863 | goto fail; | |
1864 | ||
1865 | /* now we know the gpio is valid and chip won't vanish */ | |
1866 | ||
1867 | spin_unlock_irqrestore(&gpio_lock, flags); | |
1868 | ||
9c4ba946 | 1869 | might_sleep_if(chip->can_sleep); |
c4b5be98 | 1870 | |
372e722e AC |
1871 | offset = gpio_chip_hwgpio(desc); |
1872 | return chip->set_debounce(chip, offset, debounce); | |
c4b5be98 FB |
1873 | |
1874 | fail: | |
1875 | spin_unlock_irqrestore(&gpio_lock, flags); | |
bcabdef1 | 1876 | if (status) |
6424de5a | 1877 | gpiod_dbg(desc, "%s: status %d\n", __func__, status); |
c4b5be98 FB |
1878 | |
1879 | return status; | |
1880 | } | |
79a9becd | 1881 | EXPORT_SYMBOL_GPL(gpiod_set_debounce); |
372e722e | 1882 | |
79a9becd AC |
1883 | /** |
1884 | * gpiod_is_active_low - test whether a GPIO is active-low or not | |
1885 | * @desc: the gpio descriptor to test | |
1886 | * | |
1887 | * Returns 1 if the GPIO is active-low, 0 otherwise. | |
1888 | */ | |
1889 | int gpiod_is_active_low(const struct gpio_desc *desc) | |
372e722e | 1890 | { |
79a9becd | 1891 | return test_bit(FLAG_ACTIVE_LOW, &desc->flags); |
372e722e | 1892 | } |
79a9becd | 1893 | EXPORT_SYMBOL_GPL(gpiod_is_active_low); |
d2876d08 DB |
1894 | |
1895 | /* I/O calls are only valid after configuration completed; the relevant | |
1896 | * "is this a valid GPIO" error checks should already have been done. | |
1897 | * | |
1898 | * "Get" operations are often inlinable as reading a pin value register, | |
1899 | * and masking the relevant bit in that register. | |
1900 | * | |
1901 | * When "set" operations are inlinable, they involve writing that mask to | |
1902 | * one register to set a low value, or a different register to set it high. | |
1903 | * Otherwise locking is needed, so there may be little value to inlining. | |
1904 | * | |
1905 | *------------------------------------------------------------------------ | |
1906 | * | |
1907 | * IMPORTANT!!! The hot paths -- get/set value -- assume that callers | |
1908 | * have requested the GPIO. That can include implicit requesting by | |
1909 | * a direction setting call. Marking a gpio as requested locks its chip | |
1910 | * in memory, guaranteeing that these table lookups need no more locking | |
1911 | * and that gpiochip_remove() will fail. | |
1912 | * | |
1913 | * REVISIT when debugging, consider adding some instrumentation to ensure | |
1914 | * that the GPIO was actually requested. | |
1915 | */ | |
1916 | ||
79a9becd | 1917 | static int _gpiod_get_raw_value(const struct gpio_desc *desc) |
d2876d08 DB |
1918 | { |
1919 | struct gpio_chip *chip; | |
3f397c21 | 1920 | int value; |
372e722e | 1921 | int offset; |
d2876d08 | 1922 | |
372e722e AC |
1923 | chip = desc->chip; |
1924 | offset = gpio_chip_hwgpio(desc); | |
372e722e AC |
1925 | value = chip->get ? chip->get(chip, offset) : 0; |
1926 | trace_gpio_value(desc_to_gpio(desc), 1, value); | |
3f397c21 | 1927 | return value; |
d2876d08 | 1928 | } |
372e722e | 1929 | |
d2876d08 | 1930 | /** |
79a9becd AC |
1931 | * gpiod_get_raw_value() - return a gpio's raw value |
1932 | * @desc: gpio whose value will be returned | |
d2876d08 | 1933 | * |
79a9becd AC |
1934 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding |
1935 | * its ACTIVE_LOW status. | |
1936 | * | |
1937 | * This function should be called from contexts where we cannot sleep, and will | |
1938 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 1939 | */ |
79a9becd | 1940 | int gpiod_get_raw_value(const struct gpio_desc *desc) |
d2876d08 | 1941 | { |
bcabdef1 AC |
1942 | if (!desc) |
1943 | return 0; | |
e4e449e8 | 1944 | /* Should be using gpio_get_value_cansleep() */ |
d8e0ac08 | 1945 | WARN_ON(desc->chip->can_sleep); |
79a9becd | 1946 | return _gpiod_get_raw_value(desc); |
d2876d08 | 1947 | } |
79a9becd | 1948 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value); |
372e722e | 1949 | |
79a9becd AC |
1950 | /** |
1951 | * gpiod_get_value() - return a gpio's value | |
1952 | * @desc: gpio whose value will be returned | |
1953 | * | |
1954 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
1955 | * account. | |
1956 | * | |
1957 | * This function should be called from contexts where we cannot sleep, and will | |
1958 | * complain if the GPIO chip functions potentially sleep. | |
1959 | */ | |
1960 | int gpiod_get_value(const struct gpio_desc *desc) | |
372e722e | 1961 | { |
79a9becd AC |
1962 | int value; |
1963 | if (!desc) | |
1964 | return 0; | |
1965 | /* Should be using gpio_get_value_cansleep() */ | |
1966 | WARN_ON(desc->chip->can_sleep); | |
1967 | ||
1968 | value = _gpiod_get_raw_value(desc); | |
1969 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
1970 | value = !value; | |
1971 | ||
1972 | return value; | |
372e722e | 1973 | } |
79a9becd | 1974 | EXPORT_SYMBOL_GPL(gpiod_get_value); |
d2876d08 | 1975 | |
aca5ce14 LD |
1976 | /* |
1977 | * _gpio_set_open_drain_value() - Set the open drain gpio's value. | |
79a9becd | 1978 | * @desc: gpio descriptor whose state need to be set. |
aca5ce14 LD |
1979 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
1980 | */ | |
372e722e | 1981 | static void _gpio_set_open_drain_value(struct gpio_desc *desc, int value) |
aca5ce14 LD |
1982 | { |
1983 | int err = 0; | |
372e722e AC |
1984 | struct gpio_chip *chip = desc->chip; |
1985 | int offset = gpio_chip_hwgpio(desc); | |
1986 | ||
aca5ce14 | 1987 | if (value) { |
372e722e | 1988 | err = chip->direction_input(chip, offset); |
aca5ce14 | 1989 | if (!err) |
372e722e | 1990 | clear_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 1991 | } else { |
372e722e | 1992 | err = chip->direction_output(chip, offset, 0); |
aca5ce14 | 1993 | if (!err) |
372e722e | 1994 | set_bit(FLAG_IS_OUT, &desc->flags); |
aca5ce14 | 1995 | } |
372e722e | 1996 | trace_gpio_direction(desc_to_gpio(desc), value, err); |
aca5ce14 | 1997 | if (err < 0) |
6424de5a MB |
1998 | gpiod_err(desc, |
1999 | "%s: Error in set_value for open drain err %d\n", | |
2000 | __func__, err); | |
aca5ce14 LD |
2001 | } |
2002 | ||
25553ff0 | 2003 | /* |
79a9becd AC |
2004 | * _gpio_set_open_source_value() - Set the open source gpio's value. |
2005 | * @desc: gpio descriptor whose state need to be set. | |
25553ff0 LD |
2006 | * @value: Non-zero for setting it HIGH otherise it will set to LOW. |
2007 | */ | |
372e722e | 2008 | static void _gpio_set_open_source_value(struct gpio_desc *desc, int value) |
25553ff0 LD |
2009 | { |
2010 | int err = 0; | |
372e722e AC |
2011 | struct gpio_chip *chip = desc->chip; |
2012 | int offset = gpio_chip_hwgpio(desc); | |
2013 | ||
25553ff0 | 2014 | if (value) { |
372e722e | 2015 | err = chip->direction_output(chip, offset, 1); |
25553ff0 | 2016 | if (!err) |
372e722e | 2017 | set_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 2018 | } else { |
372e722e | 2019 | err = chip->direction_input(chip, offset); |
25553ff0 | 2020 | if (!err) |
372e722e | 2021 | clear_bit(FLAG_IS_OUT, &desc->flags); |
25553ff0 | 2022 | } |
372e722e | 2023 | trace_gpio_direction(desc_to_gpio(desc), !value, err); |
25553ff0 | 2024 | if (err < 0) |
6424de5a MB |
2025 | gpiod_err(desc, |
2026 | "%s: Error in set_value for open source err %d\n", | |
2027 | __func__, err); | |
25553ff0 LD |
2028 | } |
2029 | ||
79a9becd | 2030 | static void _gpiod_set_raw_value(struct gpio_desc *desc, int value) |
d2876d08 DB |
2031 | { |
2032 | struct gpio_chip *chip; | |
2033 | ||
372e722e | 2034 | chip = desc->chip; |
372e722e AC |
2035 | trace_gpio_value(desc_to_gpio(desc), 0, value); |
2036 | if (test_bit(FLAG_OPEN_DRAIN, &desc->flags)) | |
2037 | _gpio_set_open_drain_value(desc, value); | |
2038 | else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) | |
2039 | _gpio_set_open_source_value(desc, value); | |
aca5ce14 | 2040 | else |
372e722e AC |
2041 | chip->set(chip, gpio_chip_hwgpio(desc), value); |
2042 | } | |
2043 | ||
d2876d08 | 2044 | /** |
79a9becd AC |
2045 | * gpiod_set_raw_value() - assign a gpio's raw value |
2046 | * @desc: gpio whose value will be assigned | |
d2876d08 | 2047 | * @value: value to assign |
d2876d08 | 2048 | * |
79a9becd AC |
2049 | * Set the raw value of the GPIO, i.e. the value of its physical line without |
2050 | * regard for its ACTIVE_LOW status. | |
2051 | * | |
2052 | * This function should be called from contexts where we cannot sleep, and will | |
2053 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 2054 | */ |
79a9becd | 2055 | void gpiod_set_raw_value(struct gpio_desc *desc, int value) |
372e722e | 2056 | { |
bcabdef1 AC |
2057 | if (!desc) |
2058 | return; | |
e4e449e8 | 2059 | /* Should be using gpio_set_value_cansleep() */ |
d8e0ac08 | 2060 | WARN_ON(desc->chip->can_sleep); |
79a9becd | 2061 | _gpiod_set_raw_value(desc, value); |
d2876d08 | 2062 | } |
79a9becd | 2063 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value); |
d2876d08 DB |
2064 | |
2065 | /** | |
79a9becd AC |
2066 | * gpiod_set_value() - assign a gpio's value |
2067 | * @desc: gpio whose value will be assigned | |
2068 | * @value: value to assign | |
2069 | * | |
2070 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
2071 | * account | |
d2876d08 | 2072 | * |
79a9becd AC |
2073 | * This function should be called from contexts where we cannot sleep, and will |
2074 | * complain if the GPIO chip functions potentially sleep. | |
d2876d08 | 2075 | */ |
79a9becd | 2076 | void gpiod_set_value(struct gpio_desc *desc, int value) |
d2876d08 | 2077 | { |
bcabdef1 | 2078 | if (!desc) |
79a9becd AC |
2079 | return; |
2080 | /* Should be using gpio_set_value_cansleep() */ | |
2081 | WARN_ON(desc->chip->can_sleep); | |
2082 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
2083 | value = !value; | |
2084 | _gpiod_set_raw_value(desc, value); | |
372e722e | 2085 | } |
79a9becd | 2086 | EXPORT_SYMBOL_GPL(gpiod_set_value); |
d2876d08 | 2087 | |
d2876d08 | 2088 | /** |
79a9becd AC |
2089 | * gpiod_cansleep() - report whether gpio value access may sleep |
2090 | * @desc: gpio to check | |
d2876d08 | 2091 | * |
d2876d08 | 2092 | */ |
79a9becd | 2093 | int gpiod_cansleep(const struct gpio_desc *desc) |
372e722e | 2094 | { |
bcabdef1 AC |
2095 | if (!desc) |
2096 | return 0; | |
372e722e | 2097 | return desc->chip->can_sleep; |
d2876d08 | 2098 | } |
79a9becd | 2099 | EXPORT_SYMBOL_GPL(gpiod_cansleep); |
d2876d08 | 2100 | |
0f6d504e | 2101 | /** |
79a9becd AC |
2102 | * gpiod_to_irq() - return the IRQ corresponding to a GPIO |
2103 | * @desc: gpio whose IRQ will be returned (already requested) | |
0f6d504e | 2104 | * |
79a9becd AC |
2105 | * Return the IRQ corresponding to the passed GPIO, or an error code in case of |
2106 | * error. | |
0f6d504e | 2107 | */ |
79a9becd | 2108 | int gpiod_to_irq(const struct gpio_desc *desc) |
0f6d504e DB |
2109 | { |
2110 | struct gpio_chip *chip; | |
372e722e | 2111 | int offset; |
0f6d504e | 2112 | |
bcabdef1 AC |
2113 | if (!desc) |
2114 | return -EINVAL; | |
372e722e AC |
2115 | chip = desc->chip; |
2116 | offset = gpio_chip_hwgpio(desc); | |
2117 | return chip->to_irq ? chip->to_irq(chip, offset) : -ENXIO; | |
0f6d504e | 2118 | } |
79a9becd | 2119 | EXPORT_SYMBOL_GPL(gpiod_to_irq); |
0f6d504e | 2120 | |
d468bf9e LW |
2121 | /** |
2122 | * gpiod_lock_as_irq() - lock a GPIO to be used as IRQ | |
2123 | * @gpio: the GPIO line to lock as used for IRQ | |
2124 | * | |
2125 | * This is used directly by GPIO drivers that want to lock down | |
2126 | * a certain GPIO line to be used as IRQs, for example in the | |
2127 | * .to_irq() callback of their gpio_chip, or in the .irq_enable() | |
2128 | * of its irq_chip implementation if the GPIO is known from that | |
2129 | * code. | |
2130 | */ | |
79a9becd | 2131 | int gpiod_lock_as_irq(struct gpio_desc *desc) |
372e722e | 2132 | { |
d468bf9e LW |
2133 | if (!desc) |
2134 | return -EINVAL; | |
2135 | ||
2136 | if (test_bit(FLAG_IS_OUT, &desc->flags)) { | |
2137 | gpiod_err(desc, | |
2138 | "%s: tried to flag a GPIO set as output for IRQ\n", | |
2139 | __func__); | |
2140 | return -EIO; | |
2141 | } | |
2142 | ||
2143 | set_bit(FLAG_USED_AS_IRQ, &desc->flags); | |
2144 | return 0; | |
372e722e | 2145 | } |
79a9becd | 2146 | EXPORT_SYMBOL_GPL(gpiod_lock_as_irq); |
d2876d08 | 2147 | |
d468bf9e LW |
2148 | int gpio_lock_as_irq(struct gpio_chip *chip, unsigned int offset) |
2149 | { | |
2150 | return gpiod_lock_as_irq(gpiochip_offset_to_desc(chip, offset)); | |
2151 | } | |
2152 | EXPORT_SYMBOL_GPL(gpio_lock_as_irq); | |
d2876d08 | 2153 | |
d468bf9e LW |
2154 | /** |
2155 | * gpiod_unlock_as_irq() - unlock a GPIO used as IRQ | |
2156 | * @gpio: the GPIO line to unlock from IRQ usage | |
2157 | * | |
2158 | * This is used directly by GPIO drivers that want to indicate | |
2159 | * that a certain GPIO is no longer used exclusively for IRQ. | |
d2876d08 | 2160 | */ |
79a9becd | 2161 | void gpiod_unlock_as_irq(struct gpio_desc *desc) |
d468bf9e LW |
2162 | { |
2163 | if (!desc) | |
2164 | return; | |
d2876d08 | 2165 | |
d468bf9e LW |
2166 | clear_bit(FLAG_USED_AS_IRQ, &desc->flags); |
2167 | } | |
79a9becd | 2168 | EXPORT_SYMBOL_GPL(gpiod_unlock_as_irq); |
d468bf9e LW |
2169 | |
2170 | void gpio_unlock_as_irq(struct gpio_chip *chip, unsigned int offset) | |
2171 | { | |
2172 | return gpiod_unlock_as_irq(gpiochip_offset_to_desc(chip, offset)); | |
2173 | } | |
2174 | EXPORT_SYMBOL_GPL(gpio_unlock_as_irq); | |
d2876d08 | 2175 | |
79a9becd AC |
2176 | /** |
2177 | * gpiod_get_raw_value_cansleep() - return a gpio's raw value | |
2178 | * @desc: gpio whose value will be returned | |
2179 | * | |
2180 | * Return the GPIO's raw value, i.e. the value of the physical line disregarding | |
2181 | * its ACTIVE_LOW status. | |
2182 | * | |
2183 | * This function is to be called from contexts that can sleep. | |
d2876d08 | 2184 | */ |
79a9becd | 2185 | int gpiod_get_raw_value_cansleep(const struct gpio_desc *desc) |
d2876d08 | 2186 | { |
d2876d08 | 2187 | might_sleep_if(extra_checks); |
bcabdef1 AC |
2188 | if (!desc) |
2189 | return 0; | |
79a9becd | 2190 | return _gpiod_get_raw_value(desc); |
d2876d08 | 2191 | } |
79a9becd | 2192 | EXPORT_SYMBOL_GPL(gpiod_get_raw_value_cansleep); |
372e722e | 2193 | |
79a9becd AC |
2194 | /** |
2195 | * gpiod_get_value_cansleep() - return a gpio's value | |
2196 | * @desc: gpio whose value will be returned | |
2197 | * | |
2198 | * Return the GPIO's logical value, i.e. taking the ACTIVE_LOW status into | |
2199 | * account. | |
2200 | * | |
2201 | * This function is to be called from contexts that can sleep. | |
2202 | */ | |
2203 | int gpiod_get_value_cansleep(const struct gpio_desc *desc) | |
d2876d08 | 2204 | { |
3f397c21 | 2205 | int value; |
d2876d08 DB |
2206 | |
2207 | might_sleep_if(extra_checks); | |
bcabdef1 AC |
2208 | if (!desc) |
2209 | return 0; | |
79a9becd AC |
2210 | |
2211 | value = _gpiod_get_raw_value(desc); | |
2212 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
2213 | value = !value; | |
2214 | ||
3f397c21 | 2215 | return value; |
d2876d08 | 2216 | } |
79a9becd | 2217 | EXPORT_SYMBOL_GPL(gpiod_get_value_cansleep); |
372e722e | 2218 | |
79a9becd AC |
2219 | /** |
2220 | * gpiod_set_raw_value_cansleep() - assign a gpio's raw value | |
2221 | * @desc: gpio whose value will be assigned | |
2222 | * @value: value to assign | |
2223 | * | |
2224 | * Set the raw value of the GPIO, i.e. the value of its physical line without | |
2225 | * regard for its ACTIVE_LOW status. | |
2226 | * | |
2227 | * This function is to be called from contexts that can sleep. | |
2228 | */ | |
2229 | void gpiod_set_raw_value_cansleep(struct gpio_desc *desc, int value) | |
372e722e | 2230 | { |
d2876d08 | 2231 | might_sleep_if(extra_checks); |
bcabdef1 AC |
2232 | if (!desc) |
2233 | return; | |
79a9becd | 2234 | _gpiod_set_raw_value(desc, value); |
372e722e | 2235 | } |
79a9becd | 2236 | EXPORT_SYMBOL_GPL(gpiod_set_raw_value_cansleep); |
d2876d08 | 2237 | |
79a9becd AC |
2238 | /** |
2239 | * gpiod_set_value_cansleep() - assign a gpio's value | |
2240 | * @desc: gpio whose value will be assigned | |
2241 | * @value: value to assign | |
2242 | * | |
2243 | * Set the logical value of the GPIO, i.e. taking its ACTIVE_LOW status into | |
2244 | * account | |
2245 | * | |
2246 | * This function is to be called from contexts that can sleep. | |
2247 | */ | |
2248 | void gpiod_set_value_cansleep(struct gpio_desc *desc, int value) | |
d2876d08 | 2249 | { |
d2876d08 | 2250 | might_sleep_if(extra_checks); |
bcabdef1 AC |
2251 | if (!desc) |
2252 | return; | |
79a9becd AC |
2253 | |
2254 | if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) | |
2255 | value = !value; | |
2256 | _gpiod_set_raw_value(desc, value); | |
372e722e | 2257 | } |
79a9becd | 2258 | EXPORT_SYMBOL_GPL(gpiod_set_value_cansleep); |
d2876d08 | 2259 | |
bae48da2 | 2260 | /** |
ad824783 AC |
2261 | * gpiod_add_lookup_table() - register GPIO device consumers |
2262 | * @table: table of consumers to register | |
bae48da2 | 2263 | */ |
ad824783 | 2264 | void gpiod_add_lookup_table(struct gpiod_lookup_table *table) |
bae48da2 AC |
2265 | { |
2266 | mutex_lock(&gpio_lookup_lock); | |
2267 | ||
ad824783 | 2268 | list_add_tail(&table->list, &gpio_lookup_list); |
bae48da2 AC |
2269 | |
2270 | mutex_unlock(&gpio_lookup_lock); | |
2271 | } | |
2272 | ||
bae48da2 AC |
2273 | #ifdef CONFIG_OF |
2274 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, | |
53e7cac3 AC |
2275 | unsigned int idx, |
2276 | enum gpio_lookup_flags *flags) | |
bae48da2 AC |
2277 | { |
2278 | char prop_name[32]; /* 32 is max size of property name */ | |
2279 | enum of_gpio_flags of_flags; | |
2280 | struct gpio_desc *desc; | |
2281 | ||
2282 | if (con_id) | |
2283 | snprintf(prop_name, 32, "%s-gpios", con_id); | |
aca5ce14 | 2284 | else |
bae48da2 AC |
2285 | snprintf(prop_name, 32, "gpios"); |
2286 | ||
2287 | desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx, | |
2288 | &of_flags); | |
2289 | ||
2290 | if (IS_ERR(desc)) | |
2291 | return desc; | |
2292 | ||
2293 | if (of_flags & OF_GPIO_ACTIVE_LOW) | |
53e7cac3 | 2294 | *flags |= GPIO_ACTIVE_LOW; |
bae48da2 AC |
2295 | |
2296 | return desc; | |
2297 | } | |
2298 | #else | |
2299 | static struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id, | |
209e64f9 AC |
2300 | unsigned int idx, |
2301 | enum gpio_lookup_flags *flags) | |
bae48da2 AC |
2302 | { |
2303 | return ERR_PTR(-ENODEV); | |
d2876d08 | 2304 | } |
bae48da2 | 2305 | #endif |
d2876d08 | 2306 | |
81f59e9d | 2307 | static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, |
53e7cac3 AC |
2308 | unsigned int idx, |
2309 | enum gpio_lookup_flags *flags) | |
372e722e | 2310 | { |
e01f440a MW |
2311 | struct acpi_gpio_info info; |
2312 | struct gpio_desc *desc; | |
2313 | ||
2314 | desc = acpi_get_gpiod_by_index(dev, idx, &info); | |
2315 | if (IS_ERR(desc)) | |
2316 | return desc; | |
2317 | ||
2318 | if (info.gpioint && info.active_low) | |
53e7cac3 | 2319 | *flags |= GPIO_ACTIVE_LOW; |
e01f440a MW |
2320 | |
2321 | return desc; | |
81f59e9d MW |
2322 | } |
2323 | ||
ad824783 | 2324 | static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) |
bae48da2 AC |
2325 | { |
2326 | const char *dev_id = dev ? dev_name(dev) : NULL; | |
ad824783 | 2327 | struct gpiod_lookup_table *table; |
bae48da2 AC |
2328 | |
2329 | mutex_lock(&gpio_lookup_lock); | |
2330 | ||
ad824783 AC |
2331 | list_for_each_entry(table, &gpio_lookup_list, list) { |
2332 | if (table->dev_id && dev_id) { | |
2333 | /* | |
2334 | * Valid strings on both ends, must be identical to have | |
2335 | * a match | |
2336 | */ | |
2337 | if (!strcmp(table->dev_id, dev_id)) | |
2338 | goto found; | |
2339 | } else { | |
2340 | /* | |
2341 | * One of the pointers is NULL, so both must be to have | |
2342 | * a match | |
2343 | */ | |
2344 | if (dev_id == table->dev_id) | |
2345 | goto found; | |
2346 | } | |
2347 | } | |
2348 | table = NULL; | |
bae48da2 | 2349 | |
ad824783 AC |
2350 | found: |
2351 | mutex_unlock(&gpio_lookup_lock); | |
2352 | return table; | |
2353 | } | |
bae48da2 | 2354 | |
ad824783 AC |
2355 | static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, |
2356 | unsigned int idx, | |
2357 | enum gpio_lookup_flags *flags) | |
2358 | { | |
2359 | struct gpio_desc *desc = ERR_PTR(-ENODEV); | |
2360 | struct gpiod_lookup_table *table; | |
2361 | struct gpiod_lookup *p; | |
bae48da2 | 2362 | |
ad824783 AC |
2363 | table = gpiod_find_lookup_table(dev); |
2364 | if (!table) | |
2365 | return desc; | |
bae48da2 | 2366 | |
ad824783 AC |
2367 | for (p = &table->table[0]; p->chip_label; p++) { |
2368 | struct gpio_chip *chip; | |
bae48da2 | 2369 | |
ad824783 | 2370 | /* idx must always match exactly */ |
bae48da2 AC |
2371 | if (p->idx != idx) |
2372 | continue; | |
2373 | ||
ad824783 AC |
2374 | /* If the lookup entry has a con_id, require exact match */ |
2375 | if (p->con_id && (!con_id || strcmp(p->con_id, con_id))) | |
2376 | continue; | |
bae48da2 | 2377 | |
ad824783 | 2378 | chip = find_chip_by_name(p->chip_label); |
bae48da2 | 2379 | |
ad824783 AC |
2380 | if (!chip) { |
2381 | dev_warn(dev, "cannot find GPIO chip %s\n", | |
2382 | p->chip_label); | |
2383 | continue; | |
2384 | } | |
bae48da2 | 2385 | |
ad824783 AC |
2386 | if (chip->ngpio <= p->chip_hwnum) { |
2387 | dev_warn(dev, "GPIO chip %s has %d GPIOs\n", | |
2388 | chip->label, chip->ngpio); | |
2389 | continue; | |
bae48da2 | 2390 | } |
bae48da2 | 2391 | |
ad824783 AC |
2392 | desc = gpiochip_offset_to_desc(chip, p->chip_hwnum); |
2393 | *flags = p->flags; | |
2394 | } | |
bae48da2 AC |
2395 | |
2396 | return desc; | |
2397 | } | |
2398 | ||
2399 | /** | |
2400 | * gpio_get - obtain a GPIO for a given GPIO function | |
ad824783 | 2401 | * @dev: GPIO consumer, can be NULL for system-global GPIOs |
bae48da2 AC |
2402 | * @con_id: function within the GPIO consumer |
2403 | * | |
2404 | * Return the GPIO descriptor corresponding to the function con_id of device | |
2405 | * dev, or an IS_ERR() condition if an error occured. | |
2406 | */ | |
2407 | struct gpio_desc *__must_check gpiod_get(struct device *dev, const char *con_id) | |
2408 | { | |
2409 | return gpiod_get_index(dev, con_id, 0); | |
2410 | } | |
2411 | EXPORT_SYMBOL_GPL(gpiod_get); | |
2412 | ||
2413 | /** | |
2414 | * gpiod_get_index - obtain a GPIO from a multi-index GPIO function | |
2415 | * @dev: GPIO consumer | |
2416 | * @con_id: function within the GPIO consumer | |
2417 | * @idx: index of the GPIO to obtain in the consumer | |
2418 | * | |
2419 | * This variant of gpiod_get() allows to access GPIOs other than the first | |
2420 | * defined one for functions that define several GPIOs. | |
2421 | * | |
2422 | * Return a valid GPIO descriptor, or an IS_ERR() condition in case of error. | |
2423 | */ | |
2424 | struct gpio_desc *__must_check gpiod_get_index(struct device *dev, | |
2425 | const char *con_id, | |
2426 | unsigned int idx) | |
2427 | { | |
35c5d7fd | 2428 | struct gpio_desc *desc = NULL; |
bae48da2 | 2429 | int status; |
53e7cac3 | 2430 | enum gpio_lookup_flags flags = 0; |
bae48da2 AC |
2431 | |
2432 | dev_dbg(dev, "GPIO lookup for consumer %s\n", con_id); | |
2433 | ||
2434 | /* Using device tree? */ | |
2435 | if (IS_ENABLED(CONFIG_OF) && dev && dev->of_node) { | |
2436 | dev_dbg(dev, "using device tree for GPIO lookup\n"); | |
2437 | desc = of_find_gpio(dev, con_id, idx, &flags); | |
81f59e9d MW |
2438 | } else if (IS_ENABLED(CONFIG_ACPI) && dev && ACPI_HANDLE(dev)) { |
2439 | dev_dbg(dev, "using ACPI for GPIO lookup\n"); | |
2440 | desc = acpi_find_gpio(dev, con_id, idx, &flags); | |
35c5d7fd AC |
2441 | } |
2442 | ||
2443 | /* | |
2444 | * Either we are not using DT or ACPI, or their lookup did not return | |
2445 | * a result. In that case, use platform lookup as a fallback. | |
2446 | */ | |
2447 | if (!desc || IS_ERR(desc)) { | |
2448 | struct gpio_desc *pdesc; | |
7589e59f | 2449 | |
bae48da2 | 2450 | dev_dbg(dev, "using lookup tables for GPIO lookup"); |
35c5d7fd | 2451 | pdesc = gpiod_find(dev, con_id, idx, &flags); |
7589e59f | 2452 | |
35c5d7fd AC |
2453 | /* If used as fallback, do not replace the previous error */ |
2454 | if (!IS_ERR(pdesc) || !desc) | |
2455 | desc = pdesc; | |
bae48da2 AC |
2456 | } |
2457 | ||
2458 | if (IS_ERR(desc)) { | |
351cfe0f | 2459 | dev_dbg(dev, "lookup for GPIO %s failed\n", con_id); |
bae48da2 AC |
2460 | return desc; |
2461 | } | |
2462 | ||
2463 | status = gpiod_request(desc, con_id); | |
2464 | ||
2465 | if (status < 0) | |
2466 | return ERR_PTR(status); | |
2467 | ||
53e7cac3 | 2468 | if (flags & GPIO_ACTIVE_LOW) |
bae48da2 | 2469 | set_bit(FLAG_ACTIVE_LOW, &desc->flags); |
53e7cac3 AC |
2470 | if (flags & GPIO_OPEN_DRAIN) |
2471 | set_bit(FLAG_OPEN_DRAIN, &desc->flags); | |
2472 | if (flags & GPIO_OPEN_SOURCE) | |
2473 | set_bit(FLAG_OPEN_SOURCE, &desc->flags); | |
bae48da2 AC |
2474 | |
2475 | return desc; | |
2476 | } | |
2477 | EXPORT_SYMBOL_GPL(gpiod_get_index); | |
2478 | ||
2479 | /** | |
2480 | * gpiod_put - dispose of a GPIO descriptor | |
2481 | * @desc: GPIO descriptor to dispose of | |
2482 | * | |
2483 | * No descriptor can be used after gpiod_put() has been called on it. | |
2484 | */ | |
2485 | void gpiod_put(struct gpio_desc *desc) | |
2486 | { | |
2487 | gpiod_free(desc); | |
372e722e | 2488 | } |
bae48da2 | 2489 | EXPORT_SYMBOL_GPL(gpiod_put); |
d2876d08 DB |
2490 | |
2491 | #ifdef CONFIG_DEBUG_FS | |
2492 | ||
d2876d08 DB |
2493 | static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip) |
2494 | { | |
2495 | unsigned i; | |
2496 | unsigned gpio = chip->base; | |
6c0b4e6c | 2497 | struct gpio_desc *gdesc = &chip->desc[0]; |
d2876d08 | 2498 | int is_out; |
d468bf9e | 2499 | int is_irq; |
d2876d08 DB |
2500 | |
2501 | for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) { | |
2502 | if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) | |
2503 | continue; | |
2504 | ||
372e722e | 2505 | gpiod_get_direction(gdesc); |
d2876d08 | 2506 | is_out = test_bit(FLAG_IS_OUT, &gdesc->flags); |
d468bf9e LW |
2507 | is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags); |
2508 | seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s", | |
d2876d08 DB |
2509 | gpio, gdesc->label, |
2510 | is_out ? "out" : "in ", | |
2511 | chip->get | |
2512 | ? (chip->get(chip, i) ? "hi" : "lo") | |
d468bf9e LW |
2513 | : "? ", |
2514 | is_irq ? "IRQ" : " "); | |
d2876d08 DB |
2515 | seq_printf(s, "\n"); |
2516 | } | |
2517 | } | |
2518 | ||
f9c4a31f | 2519 | static void *gpiolib_seq_start(struct seq_file *s, loff_t *pos) |
d2876d08 | 2520 | { |
362432ae | 2521 | unsigned long flags; |
f9c4a31f | 2522 | struct gpio_chip *chip = NULL; |
cb1650d4 | 2523 | loff_t index = *pos; |
d2876d08 | 2524 | |
f9c4a31f | 2525 | s->private = ""; |
d2876d08 | 2526 | |
362432ae | 2527 | spin_lock_irqsave(&gpio_lock, flags); |
cb1650d4 | 2528 | list_for_each_entry(chip, &gpio_chips, list) |
362432ae GL |
2529 | if (index-- == 0) { |
2530 | spin_unlock_irqrestore(&gpio_lock, flags); | |
cb1650d4 | 2531 | return chip; |
f9c4a31f | 2532 | } |
362432ae | 2533 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f | 2534 | |
cb1650d4 | 2535 | return NULL; |
f9c4a31f TR |
2536 | } |
2537 | ||
2538 | static void *gpiolib_seq_next(struct seq_file *s, void *v, loff_t *pos) | |
2539 | { | |
362432ae | 2540 | unsigned long flags; |
f9c4a31f | 2541 | struct gpio_chip *chip = v; |
f9c4a31f TR |
2542 | void *ret = NULL; |
2543 | ||
362432ae | 2544 | spin_lock_irqsave(&gpio_lock, flags); |
cb1650d4 AC |
2545 | if (list_is_last(&chip->list, &gpio_chips)) |
2546 | ret = NULL; | |
2547 | else | |
2548 | ret = list_entry(chip->list.next, struct gpio_chip, list); | |
362432ae | 2549 | spin_unlock_irqrestore(&gpio_lock, flags); |
f9c4a31f TR |
2550 | |
2551 | s->private = "\n"; | |
2552 | ++*pos; | |
2553 | ||
2554 | return ret; | |
2555 | } | |
2556 | ||
2557 | static void gpiolib_seq_stop(struct seq_file *s, void *v) | |
2558 | { | |
2559 | } | |
2560 | ||
2561 | static int gpiolib_seq_show(struct seq_file *s, void *v) | |
2562 | { | |
2563 | struct gpio_chip *chip = v; | |
2564 | struct device *dev; | |
2565 | ||
2566 | seq_printf(s, "%sGPIOs %d-%d", (char *)s->private, | |
2567 | chip->base, chip->base + chip->ngpio - 1); | |
2568 | dev = chip->dev; | |
2569 | if (dev) | |
2570 | seq_printf(s, ", %s/%s", dev->bus ? dev->bus->name : "no-bus", | |
2571 | dev_name(dev)); | |
2572 | if (chip->label) | |
2573 | seq_printf(s, ", %s", chip->label); | |
2574 | if (chip->can_sleep) | |
2575 | seq_printf(s, ", can sleep"); | |
2576 | seq_printf(s, ":\n"); | |
2577 | ||
2578 | if (chip->dbg_show) | |
2579 | chip->dbg_show(s, chip); | |
2580 | else | |
2581 | gpiolib_dbg_show(s, chip); | |
2582 | ||
d2876d08 DB |
2583 | return 0; |
2584 | } | |
2585 | ||
f9c4a31f TR |
2586 | static const struct seq_operations gpiolib_seq_ops = { |
2587 | .start = gpiolib_seq_start, | |
2588 | .next = gpiolib_seq_next, | |
2589 | .stop = gpiolib_seq_stop, | |
2590 | .show = gpiolib_seq_show, | |
2591 | }; | |
2592 | ||
d2876d08 DB |
2593 | static int gpiolib_open(struct inode *inode, struct file *file) |
2594 | { | |
f9c4a31f | 2595 | return seq_open(file, &gpiolib_seq_ops); |
d2876d08 DB |
2596 | } |
2597 | ||
828c0950 | 2598 | static const struct file_operations gpiolib_operations = { |
f9c4a31f | 2599 | .owner = THIS_MODULE, |
d2876d08 DB |
2600 | .open = gpiolib_open, |
2601 | .read = seq_read, | |
2602 | .llseek = seq_lseek, | |
f9c4a31f | 2603 | .release = seq_release, |
d2876d08 DB |
2604 | }; |
2605 | ||
2606 | static int __init gpiolib_debugfs_init(void) | |
2607 | { | |
2608 | /* /sys/kernel/debug/gpio */ | |
2609 | (void) debugfs_create_file("gpio", S_IFREG | S_IRUGO, | |
2610 | NULL, NULL, &gpiolib_operations); | |
2611 | return 0; | |
2612 | } | |
2613 | subsys_initcall(gpiolib_debugfs_init); | |
2614 | ||
2615 | #endif /* DEBUG_FS */ |