blob: c5a5b57463c75c737ce04a547c8f9cebcde31e2c [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
Linus Walleij3c702e92015-10-21 15:29:53 +020029 * @chrdev: character device for the GPIO device
Linus Walleijafbc4f32016-02-09 13:21:06 +010030 * @mockdev: class device used by the deprecated sysfs interface (may be
31 * NULL)
Linus Walleijff2b1352015-10-20 11:10:38 +020032 * @owner: helps prevent removal of modules exporting active GPIOs
33 * @chip: pointer to the corresponding gpiochip, holding static
34 * data for this device
35 * @list: links gpio_device:s together for traversal
36 *
37 * This state container holds most of the runtime variable data
38 * for a GPIO device and can hold references and live on after the
39 * GPIO chip has been removed, if it is still being used from
40 * userspace.
41 */
42struct gpio_device {
43 int id;
44 struct device dev;
Linus Walleij3c702e92015-10-21 15:29:53 +020045 struct cdev chrdev;
Linus Walleijafbc4f32016-02-09 13:21:06 +010046 struct device *mockdev;
Linus Walleijff2b1352015-10-20 11:10:38 +020047 struct module *owner;
48 struct gpio_chip *chip;
49 struct list_head list;
50};
51
52/**
Mika Westerberg5ccff852014-01-08 12:40:56 +020053 * struct acpi_gpio_info - ACPI GPIO specific information
54 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
55 * @active_low: in case of @gpioint, the pin is active low
56 */
57struct acpi_gpio_info {
58 bool gpioint;
Christophe RICARD52044722015-12-23 23:25:34 +010059 int polarity;
60 int triggering;
Mika Westerberg5ccff852014-01-08 12:40:56 +020061};
62
Rojhalat Ibrahim7f2e5532015-02-11 17:27:55 +010063/* gpio suffixes used for ACPI and device tree lookup */
64static const char * const gpio_suffixes[] = { "gpios", "gpio" };
65
Mika Westerberg664e3e52014-01-08 12:40:54 +020066#ifdef CONFIG_ACPI
67void acpi_gpiochip_add(struct gpio_chip *chip);
68void acpi_gpiochip_remove(struct gpio_chip *chip);
Mika Westerberg5ccff852014-01-08 12:40:56 +020069
Mika Westerbergafa82fa2014-07-25 09:54:48 +030070void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
71void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);
72
Mika Westerberg0d9a6932014-10-29 15:41:01 +010073struct gpio_desc *acpi_get_gpiod_by_index(struct acpi_device *adev,
74 const char *propname, int index,
Mika Westerberg5ccff852014-01-08 12:40:56 +020075 struct acpi_gpio_info *info);
Rafael J. Wysocki504a3372015-08-27 04:42:33 +020076struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
77 const char *propname, int index,
78 struct acpi_gpio_info *info);
Rojhalat Ibrahim66858522015-02-11 17:27:58 +010079
80int acpi_gpio_count(struct device *dev, const char *con_id);
Dmitry Torokhov10cf4892015-11-11 11:45:30 -080081
82bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
Mika Westerberg664e3e52014-01-08 12:40:54 +020083#else
84static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
85static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
Mika Westerberg5ccff852014-01-08 12:40:56 +020086
Mika Westerbergafa82fa2014-07-25 09:54:48 +030087static inline void
88acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }
89
90static inline void
91acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }
92
Mika Westerberg5ccff852014-01-08 12:40:56 +020093static inline struct gpio_desc *
Mika Westerberg0d9a6932014-10-29 15:41:01 +010094acpi_get_gpiod_by_index(struct acpi_device *adev, const char *propname,
95 int index, struct acpi_gpio_info *info)
Mika Westerberg5ccff852014-01-08 12:40:56 +020096{
97 return ERR_PTR(-ENOSYS);
98}
Rafael J. Wysocki504a3372015-08-27 04:42:33 +020099static inline struct gpio_desc *
100acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
101 int index, struct acpi_gpio_info *info)
102{
103 return ERR_PTR(-ENXIO);
104}
Rojhalat Ibrahim66858522015-02-11 17:27:58 +0100105static inline int acpi_gpio_count(struct device *dev, const char *con_id)
106{
107 return -ENODEV;
108}
Dmitry Torokhov10cf4892015-11-11 11:45:30 -0800109
110static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
111 const char *con_id)
112{
113 return false;
114}
Mika Westerberg664e3e52014-01-08 12:40:54 +0200115#endif
116
Alexandre Courbotf01d9072014-05-17 14:54:50 +0900117struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
118 const char *list_name, int index, enum of_gpio_flags *flags);
119
Alexandre Courbot1bd6b602014-07-22 16:17:41 +0900120struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
121
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900122extern struct spinlock gpio_lock;
Linus Walleijff2b1352015-10-20 11:10:38 +0200123extern struct list_head gpio_devices;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900124
125struct gpio_desc {
126 struct gpio_chip *chip;
127 unsigned long flags;
128/* flag symbols are bit numbers */
129#define FLAG_REQUESTED 0
130#define FLAG_IS_OUT 1
131#define FLAG_EXPORT 2 /* protected by sysfs_lock */
132#define FLAG_SYSFS 3 /* exported via /sys/class/gpio/control */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900133#define FLAG_ACTIVE_LOW 6 /* value has active low */
134#define FLAG_OPEN_DRAIN 7 /* Gpio is open drain type */
135#define FLAG_OPEN_SOURCE 8 /* Gpio is open source type */
136#define FLAG_USED_AS_IRQ 9 /* GPIO is connected to an IRQ */
Benoit Parrotf625d462015-02-02 11:44:44 -0600137#define FLAG_IS_HOGGED 11 /* GPIO is hogged */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900138
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200139 /* Connection label */
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900140 const char *label;
Markus Pargmannc0017ed2015-08-14 16:10:59 +0200141 /* Name of the GPIO */
142 const char *name;
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900143};
144
145int gpiod_request(struct gpio_desc *desc, const char *label);
146void gpiod_free(struct gpio_desc *desc);
Benoit Parrotf625d462015-02-02 11:44:44 -0600147int gpiod_hog(struct gpio_desc *desc, const char *name,
148 unsigned long lflags, enum gpiod_flags dflags);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900149
150/*
151 * Return the GPIO number of the passed descriptor relative to its chip
152 */
153static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
154{
155 return desc - &desc->chip->desc[0];
156}
157
158/* With descriptor prefix */
159
160#define gpiod_emerg(desc, fmt, ...) \
161 pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
162 ##__VA_ARGS__)
163#define gpiod_crit(desc, fmt, ...) \
164 pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
165 ##__VA_ARGS__)
166#define gpiod_err(desc, fmt, ...) \
167 pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
168 ##__VA_ARGS__)
169#define gpiod_warn(desc, fmt, ...) \
170 pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
171 ##__VA_ARGS__)
172#define gpiod_info(desc, fmt, ...) \
173 pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
174 ##__VA_ARGS__)
175#define gpiod_dbg(desc, fmt, ...) \
176 pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
177 ##__VA_ARGS__)
178
179/* With chip prefix */
180
181#define chip_emerg(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200182 dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900183#define chip_crit(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200184 dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900185#define chip_err(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200186 dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900187#define chip_warn(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200188 dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900189#define chip_info(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200190 dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900191#define chip_dbg(chip, fmt, ...) \
Linus Walleij34ffd852015-10-20 11:31:54 +0200192 dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900193
194#ifdef CONFIG_GPIO_SYSFS
195
Linus Walleijafbc4f32016-02-09 13:21:06 +0100196int gpiochip_sysfs_register(struct gpio_device *gdev);
197void gpiochip_sysfs_unregister(struct gpio_device *gdev);
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900198
199#else
200
Linus Walleijafbc4f32016-02-09 13:21:06 +0100201static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900202{
203 return 0;
204}
205
Linus Walleijafbc4f32016-02-09 13:21:06 +0100206static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
Alexandre Courbot0eb4c6c2014-07-01 14:45:15 +0900207{
208}
209
210#endif /* CONFIG_GPIO_SYSFS */
211
Mika Westerberg664e3e52014-01-08 12:40:54 +0200212#endif /* GPIOLIB_H */