blob: 3f329c922f5bd62710095184e74fc8b94a52a777 [file] [log] [blame]
Mika Westerberg664e3e52014-01-08 12:40:54 +02001/*
2 * Internal GPIO functions.
3 *
4 * Copyright (C) 2013, Intel Corporation
5 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#ifndef GPIOLIB_H
13#define GPIOLIB_H
14
Linus Walleijff2b1352015-10-20 11:10:38 +020015#include <linux/gpio/driver.h>
Mika Westerberg5ccff852014-01-08 12:40:56 +020016#include <linux/err.h>
17#include <linux/device.h>
Linus Walleijff2b1352015-10-20 11:10:38 +020018#include <linux/module.h>
19#include <linux/cdev.h>
Mika Westerberg5ccff852014-01-08 12:40:56 +020020
Alexandre Courbotf01d9072014-05-17 14:54:50 +090021enum of_gpio_flags;
Linus Walleij29ab8752015-12-08 22:45:10 +010022enum gpiod_flags;
Rafael J. Wysockice793482015-03-16 23:49:03 +010023struct acpi_device;
24
Mika Westerberg5ccff852014-01-08 12:40:56 +020025/**
Linus Walleijff2b1352015-10-20 11:10:38 +020026 * struct gpio_device - internal state container for GPIO devices
27 * @id: numerical ID number for the GPIO chip
28 * @dev: the GPIO device struct
29 * @owner: helps prevent removal of modules exporting active GPIOs
30 * @chip: pointer to the corresponding gpiochip, holding static
31 * data for this device
32 * @list: links gpio_device:s together for traversal
33 *
34 * This state container holds most of the runtime variable data
35 * for a GPIO device and can hold references and live on after the
36 * GPIO chip has been removed, if it is still being used from
37 * userspace.
38 */
39struct gpio_device {
40 int id;
41 struct device dev;
42 struct module *owner;
43 struct gpio_chip *chip;
44 struct list_head list;
45};
46
47/**
Mika Westerberg5ccff852014-01-08 12:40:56 +020048 * struct acpi_gpio_info - ACPI GPIO specific information
49 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
50 * @active_low: in case of @gpioint, the pin is active low
51 */
52struct acpi_gpio_info {
53 bool gpioint;
Christophe RICARD52044722015-12-23 23:25:34 +010054 int polarity;
55 int triggering;
Mika Westerberg5ccff852014-01-08 12:40:56 +020056};
57
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +010058/* gpio suffixes used for ACPI and device tree lookup */
59static const char * const gpio_suffixes[] = { "gpios", "gpio" };
60
Mika Westerberg664e3e52014-01-08 12:40:54 +020061#ifdef CONFIG_ACPI
62void acpi_gpiochip_add(struct gpio_chip *chip);
63void acpi_gpiochip_remove(struct gpio_chip *chip);
Mika Westerberg5ccff852014-01-08 12:40:56 +020064
Mika Westerbergafa82fa2014-07-25 09:54:48 +030065void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
66void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
67
Mika Westerberg0d9a6932014-10-29 15:41:01 +010068struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
69 const char *propname, int index,
Mika Westerberg5ccff852014-01-08 12:40:56 +020070 struct acpi_gpio_info *info);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +020071struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
72 const char *propname, int index,
73 struct acpi_gpio_info *info);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +010074
75int acpi_gpio_count(struct device *dev, const char *con_id);
Dmitry Torokhov10cf4892015-11-11 11:45:30 -080076
77bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
Mika Westerberg664e3e52014-01-08 12:40:54 +020078#else
79static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
80static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
Mika Westerberg5ccff852014-01-08 12:40:56 +020081
Mika Westerbergafa82fa2014-07-25 09:54:48 +030082static inline void
83acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
84
85static inline void
86acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
87
Mika Westerberg5ccff852014-01-08 12:40:56 +020088static inline struct gpio_desc *
Mika Westerberg0d9a6932014-10-29 15:41:01 +010089acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
90 int index, struct acpi_gpio_info *info)
Mika Westerberg5ccff852014-01-08 12:40:56 +020091{
92 return ERR_PTR(-ENOSYS);
93}
Rafael J. Wysocki504a3372015-08-27 04:42:33 +020094static inline struct gpio_desc *
95acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
96 int index, struct acpi_gpio_info *info)
97{
98 return ERR_PTR(-ENXIO);
99}
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100100static inline int acpi_gpio_count(struct device *dev, const char *con_id)
101{
102 return -ENODEV;
103}
Dmitry Torokhov10cf4892015-11-11 11:45:30 -0800104
105static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
106 const char *con_id)
107{
108 return false;
109}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200110#endif
111
Alexandre Courbotf01d9072014-05-17 14:54:50 +0900112struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
113 const char *list_name, int index, enum of_gpio_flags *flags);
114
Alexandre Courbot1bd6b602014-07-22 16:17:41 +0900115struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
116
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900117extern struct spinlock gpio_lock;
Linus Walleijff2b1352015-10-20 11:10:38 +0200118extern struct list_head gpio_devices;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900119
120struct gpio_desc {
121 struct gpio_chip *chip;
122 unsigned long flags;
123/* flag symbols are bit numbers */
124#define FLAG_REQUESTED 0
125#define FLAG_IS_OUT 1
126#define FLAG_EXPORT 2 /* protected by sysfs_lock */
127#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900128#define FLAG_ACTIVE_LOW 6 /* value has active low */
129#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
130#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
131#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Benoit Parrotf625d462015-02-02 11:44:44 -0600132#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900133
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200134 /* Connection label */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900135 const char *label;
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200136 /* Name of the GPIO */
137 const char *name;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900138};
139
140int gpiod_request(struct gpio_desc *desc, const char *label);
141void gpiod_free(struct gpio_desc *desc);
Benoit Parrotf625d462015-02-02 11:44:44 -0600142int gpiod_hog(struct gpio_desc *desc, const char *name,
143 unsigned long lflags, enum gpiod_flags dflags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900144
145/*
146 * Return the GPIO number of the passed descriptor relative to its chip
147 */
148static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
149{
150 return desc - &desc->chip->desc[0];
151}
152
153/* With descriptor prefix */
154
155#define gpiod_emerg(desc, fmt, ...) \
156 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
157 ##__VA_ARGS__)
158#define gpiod_crit(desc, fmt, ...) \
159 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
160 ##__VA_ARGS__)
161#define gpiod_err(desc, fmt, ...) \
162 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
163 ##__VA_ARGS__)
164#define gpiod_warn(desc, fmt, ...) \
165 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
166 ##__VA_ARGS__)
167#define gpiod_info(desc, fmt, ...) \
168 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
169 ##__VA_ARGS__)
170#define gpiod_dbg(desc, fmt, ...) \
171 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
172 ##__VA_ARGS__)
173
174/* With chip prefix */
175
176#define chip_emerg(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200177 dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900178#define chip_crit(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200179 dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900180#define chip_err(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200181 dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900182#define chip_warn(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200183 dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900184#define chip_info(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200185 dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900186#define chip_dbg(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200187 dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900188
189#ifdef CONFIG_GPIO_SYSFS
190
Johan Hovold426577b2015-05-04 17:10:32 +0200191int gpiochip_sysfs_register(struct gpio_chip *chip);
192void gpiochip_sysfs_unregister(struct gpio_chip *chip);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900193
194#else
195
Johan Hovold426577b2015-05-04 17:10:32 +0200196static inline int gpiochip_sysfs_register(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900197{
198 return 0;
199}
200
Johan Hovold426577b2015-05-04 17:10:32 +0200201static inline void gpiochip_sysfs_unregister(struct gpio_chip *chip)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900202{
203}
204
205#endif /* CONFIG_GPIO_SYSFS */
206
Mika Westerberg664e3e52014-01-08 12:40:54 +0200207#endif /* GPIOLIB_H */