1664e3e5aSMika Westerberg /* 2664e3e5aSMika Westerberg * Internal GPIO functions. 3664e3e5aSMika Westerberg * 4664e3e5aSMika Westerberg * Copyright (C) 2013, Intel Corporation 5664e3e5aSMika Westerberg * Author: Mika Westerberg <mika.westerberg@linux.intel.com> 6664e3e5aSMika Westerberg * 7664e3e5aSMika Westerberg * This program is free software; you can redistribute it and/or modify 8664e3e5aSMika Westerberg * it under the terms of the GNU General Public License version 2 as 9664e3e5aSMika Westerberg * published by the Free Software Foundation. 10664e3e5aSMika Westerberg */ 11664e3e5aSMika Westerberg 12664e3e5aSMika Westerberg #ifndef GPIOLIB_H 13664e3e5aSMika Westerberg #define GPIOLIB_H 14664e3e5aSMika Westerberg 155ccff852SMika Westerberg #include <linux/err.h> 165ccff852SMika Westerberg #include <linux/device.h> 175ccff852SMika Westerberg 18f01d9075SAlexandre Courbot enum of_gpio_flags; 19f01d9075SAlexandre Courbot 205ccff852SMika Westerberg /** 215ccff852SMika Westerberg * struct acpi_gpio_info - ACPI GPIO specific information 225ccff852SMika Westerberg * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo 235ccff852SMika Westerberg * @active_low: in case of @gpioint, the pin is active low 245ccff852SMika Westerberg */ 255ccff852SMika Westerberg struct acpi_gpio_info { 265ccff852SMika Westerberg bool gpioint; 275ccff852SMika Westerberg bool active_low; 285ccff852SMika Westerberg }; 295ccff852SMika Westerberg 30664e3e5aSMika Westerberg #ifdef CONFIG_ACPI 31664e3e5aSMika Westerberg void acpi_gpiochip_add(struct gpio_chip *chip); 32664e3e5aSMika Westerberg void acpi_gpiochip_remove(struct gpio_chip *chip); 335ccff852SMika Westerberg 34afa82fabSMika Westerberg void acpi_gpiochip_request_interrupts(struct gpio_chip *chip); 35afa82fabSMika Westerberg void acpi_gpiochip_free_interrupts(struct gpio_chip *chip); 36afa82fabSMika Westerberg 370d9a693cSMika Westerberg struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev, 380d9a693cSMika Westerberg const char *propname, int index, 395ccff852SMika Westerberg struct acpi_gpio_info *info); 40664e3e5aSMika Westerberg #else 41664e3e5aSMika Westerberg static inline void acpi_gpiochip_add(struct gpio_chip *chip) { } 42664e3e5aSMika Westerberg static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { } 435ccff852SMika Westerberg 44afa82fabSMika Westerberg static inline void 45afa82fabSMika Westerberg acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { } 46afa82fabSMika Westerberg 47afa82fabSMika Westerberg static inline void 48afa82fabSMika Westerberg acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { } 49afa82fabSMika Westerberg 505ccff852SMika Westerberg static inline struct gpio_desc * 510d9a693cSMika Westerberg acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname, 520d9a693cSMika Westerberg int index, struct acpi_gpio_info *info) 535ccff852SMika Westerberg { 545ccff852SMika Westerberg return ERR_PTR(-ENOSYS); 555ccff852SMika Westerberg } 56664e3e5aSMika Westerberg #endif 57664e3e5aSMika Westerberg 58f01d9075SAlexandre Courbot struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np, 59f01d9075SAlexandre Courbot const char *list_name, int index, enum of_gpio_flags *flags); 60f01d9075SAlexandre Courbot 611bd6b601SAlexandre Courbot struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum); 621bd6b601SAlexandre Courbot 630eb4c6c2SAlexandre Courbot extern struct spinlock gpio_lock; 640eb4c6c2SAlexandre Courbot extern struct list_head gpio_chips; 650eb4c6c2SAlexandre Courbot 660eb4c6c2SAlexandre Courbot struct gpio_desc { 670eb4c6c2SAlexandre Courbot struct gpio_chip *chip; 680eb4c6c2SAlexandre Courbot unsigned long flags; 690eb4c6c2SAlexandre Courbot /* flag symbols are bit numbers */ 700eb4c6c2SAlexandre Courbot #define FLAG_REQUESTED 0 710eb4c6c2SAlexandre Courbot #define FLAG_IS_OUT 1 720eb4c6c2SAlexandre Courbot #define FLAG_EXPORT 2 /* protected by sysfs_lock */ 730eb4c6c2SAlexandre Courbot #define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */ 740eb4c6c2SAlexandre Courbot #define FLAG_TRIG_FALL 4 /* trigger on falling edge */ 750eb4c6c2SAlexandre Courbot #define FLAG_TRIG_RISE 5 /* trigger on rising edge */ 760eb4c6c2SAlexandre Courbot #define FLAG_ACTIVE_LOW 6 /* value has active low */ 770eb4c6c2SAlexandre Courbot #define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */ 780eb4c6c2SAlexandre Courbot #define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */ 790eb4c6c2SAlexandre Courbot #define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */ 80ebbeba12SJohan Hovold #define FLAG_SYSFS_DIR 10 /* show sysfs direction attribute */ 810eb4c6c2SAlexandre Courbot 820eb4c6c2SAlexandre Courbot #define ID_SHIFT 16 /* add new flags before this one */ 830eb4c6c2SAlexandre Courbot 840eb4c6c2SAlexandre Courbot #define GPIO_FLAGS_MASK ((1 << ID_SHIFT) - 1) 850eb4c6c2SAlexandre Courbot #define GPIO_TRIGGER_MASK (BIT(FLAG_TRIG_FALL) | BIT(FLAG_TRIG_RISE)) 860eb4c6c2SAlexandre Courbot 870eb4c6c2SAlexandre Courbot const char *label; 880eb4c6c2SAlexandre Courbot }; 890eb4c6c2SAlexandre Courbot 900eb4c6c2SAlexandre Courbot int gpiod_request(struct gpio_desc *desc, const char *label); 910eb4c6c2SAlexandre Courbot void gpiod_free(struct gpio_desc *desc); 920eb4c6c2SAlexandre Courbot 930eb4c6c2SAlexandre Courbot /* 940eb4c6c2SAlexandre Courbot * Return the GPIO number of the passed descriptor relative to its chip 950eb4c6c2SAlexandre Courbot */ 960eb4c6c2SAlexandre Courbot static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc) 970eb4c6c2SAlexandre Courbot { 980eb4c6c2SAlexandre Courbot return desc - &desc->chip->desc[0]; 990eb4c6c2SAlexandre Courbot } 1000eb4c6c2SAlexandre Courbot 1010eb4c6c2SAlexandre Courbot /* With descriptor prefix */ 1020eb4c6c2SAlexandre Courbot 1030eb4c6c2SAlexandre Courbot #define gpiod_emerg(desc, fmt, ...) \ 1040eb4c6c2SAlexandre Courbot pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 1050eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1060eb4c6c2SAlexandre Courbot #define gpiod_crit(desc, fmt, ...) \ 1070eb4c6c2SAlexandre Courbot pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1080eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1090eb4c6c2SAlexandre Courbot #define gpiod_err(desc, fmt, ...) \ 1100eb4c6c2SAlexandre Courbot pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1110eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1120eb4c6c2SAlexandre Courbot #define gpiod_warn(desc, fmt, ...) \ 1130eb4c6c2SAlexandre Courbot pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1140eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1150eb4c6c2SAlexandre Courbot #define gpiod_info(desc, fmt, ...) \ 1160eb4c6c2SAlexandre Courbot pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \ 1170eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1180eb4c6c2SAlexandre Courbot #define gpiod_dbg(desc, fmt, ...) \ 1190eb4c6c2SAlexandre Courbot pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\ 1200eb4c6c2SAlexandre Courbot ##__VA_ARGS__) 1210eb4c6c2SAlexandre Courbot 1220eb4c6c2SAlexandre Courbot /* With chip prefix */ 1230eb4c6c2SAlexandre Courbot 1240eb4c6c2SAlexandre Courbot #define chip_emerg(chip, fmt, ...) \ 1250eb4c6c2SAlexandre Courbot pr_emerg("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 1260eb4c6c2SAlexandre Courbot #define chip_crit(chip, fmt, ...) \ 1270eb4c6c2SAlexandre Courbot pr_crit("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 1280eb4c6c2SAlexandre Courbot #define chip_err(chip, fmt, ...) \ 1290eb4c6c2SAlexandre Courbot pr_err("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 1300eb4c6c2SAlexandre Courbot #define chip_warn(chip, fmt, ...) \ 1310eb4c6c2SAlexandre Courbot pr_warn("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 1320eb4c6c2SAlexandre Courbot #define chip_info(chip, fmt, ...) \ 1330eb4c6c2SAlexandre Courbot pr_info("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 1340eb4c6c2SAlexandre Courbot #define chip_dbg(chip, fmt, ...) \ 1350eb4c6c2SAlexandre Courbot pr_debug("GPIO chip %s: " fmt, chip->label, ##__VA_ARGS__) 1360eb4c6c2SAlexandre Courbot 1370eb4c6c2SAlexandre Courbot #ifdef CONFIG_GPIO_SYSFS 1380eb4c6c2SAlexandre Courbot 1390eb4c6c2SAlexandre Courbot int gpiochip_export(struct gpio_chip *chip); 1400eb4c6c2SAlexandre Courbot void gpiochip_unexport(struct gpio_chip *chip); 1410eb4c6c2SAlexandre Courbot 1420eb4c6c2SAlexandre Courbot #else 1430eb4c6c2SAlexandre Courbot 1440eb4c6c2SAlexandre Courbot static inline int gpiochip_export(struct gpio_chip *chip) 1450eb4c6c2SAlexandre Courbot { 1460eb4c6c2SAlexandre Courbot return 0; 1470eb4c6c2SAlexandre Courbot } 1480eb4c6c2SAlexandre Courbot 1490eb4c6c2SAlexandre Courbot static inline void gpiochip_unexport(struct gpio_chip *chip) 1500eb4c6c2SAlexandre Courbot { 1510eb4c6c2SAlexandre Courbot } 1520eb4c6c2SAlexandre Courbot 1530eb4c6c2SAlexandre Courbot #endif /* CONFIG_GPIO_SYSFS */ 1540eb4c6c2SAlexandre Courbot 155664e3e5aSMika Westerberg #endif /* GPIOLIB_H */ 156