gpiolib.h 8.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Internal GPIO functions.
 *
 * Copyright (C) 2013, Intel Corporation
 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#ifndef GPIOLIB_H
#define GPIOLIB_H

15
#include <linux/gpio/driver.h>
16 17
#include <linux/err.h>
#include <linux/device.h>
18 19
#include <linux/module.h>
#include <linux/cdev.h>
20

21
enum of_gpio_flags;
22
enum gpiod_flags;
23
enum gpio_lookup_flags;
24 25
struct acpi_device;

26 27 28 29
/**
 * struct gpio_device - internal state container for GPIO devices
 * @id: numerical ID number for the GPIO chip
 * @dev: the GPIO device struct
30
 * @chrdev: character device for the GPIO device
31 32
 * @mockdev: class device used by the deprecated sysfs interface (may be
 * NULL)
33 34 35
 * @owner: helps prevent removal of modules exporting active GPIOs
 * @chip: pointer to the corresponding gpiochip, holding static
 * data for this device
36
 * @descs: array of ngpio descriptors.
37 38 39 40
 * @ngpio: the number of GPIO lines on this GPIO device, equal to the size
 * of the @descs array.
 * @base: GPIO base in the DEPRECATED global Linux GPIO numberspace, assigned
 * at device creation time.
41 42
 * @label: a descriptive name for the GPIO device, such as the part number
 * or name of the IP component in a System on Chip.
43
 * @data: per-instance data assigned by the driver
44 45 46 47 48 49 50 51 52 53
 * @list: links gpio_device:s together for traversal
 *
 * This state container holds most of the runtime variable data
 * for a GPIO device and can hold references and live on after the
 * GPIO chip has been removed, if it is still being used from
 * userspace.
 */
struct gpio_device {
	int			id;
	struct device		dev;
54
	struct cdev		chrdev;
55
	struct device		*mockdev;
56 57
	struct module		*owner;
	struct gpio_chip	*chip;
58
	struct gpio_desc	*descs;
59 60
	int			base;
	u16			ngpio;
61
	char			*label;
62
	void			*data;
63
	struct list_head        list;
64 65 66 67 68 69 70 71 72 73

#ifdef CONFIG_PINCTRL
	/*
	 * If CONFIG_PINCTRL is enabled, then gpio controllers can optionally
	 * describe the actual pin range which they serve in an SoC. This
	 * information would be used by pinctrl subsystem to configure
	 * corresponding pins for gpio usage.
	 */
	struct list_head pin_ranges;
#endif
74 75
};

76 77 78
/**
 * struct acpi_gpio_info - ACPI GPIO specific information
 * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
79 80
 * @polarity: interrupt polarity as provided by ACPI
 * @triggering: triggering type as provided by ACPI
81 82 83
 */
struct acpi_gpio_info {
	bool gpioint;
84 85
	int polarity;
	int triggering;
86 87
};

88 89 90
/* gpio suffixes used for ACPI and device tree lookup */
static const char * const gpio_suffixes[] = { "gpios", "gpio" };

91 92 93 94 95
#ifdef CONFIG_OF_GPIO
struct gpio_desc *of_find_gpio(struct device *dev,
			       const char *con_id,
			       unsigned int idx,
			       enum gpio_lookup_flags *flags);
96 97
struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
		   const char *list_name, int index, enum of_gpio_flags *flags);
98 99
int of_gpiochip_add(struct gpio_chip *gc);
void of_gpiochip_remove(struct gpio_chip *gc);
100 101 102 103 104 105 106 107
#else
static inline struct gpio_desc *of_find_gpio(struct device *dev,
					     const char *con_id,
					     unsigned int idx,
					     enum gpio_lookup_flags *flags)
{
	return ERR_PTR(-ENOENT);
}
108 109 110 111 112
static inline struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
		   const char *list_name, int index, enum of_gpio_flags *flags)
{
	return ERR_PTR(-ENOENT);
}
113 114
static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; }
static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
115 116
#endif /* CONFIG_OF_GPIO */

117 118 119
#ifdef CONFIG_ACPI
void acpi_gpiochip_add(struct gpio_chip *chip);
void acpi_gpiochip_remove(struct gpio_chip *chip);
120

121 122 123
void acpi_gpiochip_request_interrupts(struct gpio_chip *chip);
void acpi_gpiochip_free_interrupts(struct gpio_chip *chip);

124 125 126 127 128
struct gpio_desc *acpi_find_gpio(struct device *dev,
				 const char *con_id,
				 unsigned int idx,
				 enum gpiod_flags flags,
				 enum gpio_lookup_flags *lookupflags);
129 130 131
struct gpio_desc *acpi_node_get_gpiod(struct fwnode_handle *fwnode,
				      const char *propname, int index,
				      struct acpi_gpio_info *info);
132 133

int acpi_gpio_count(struct device *dev, const char *con_id);
134 135

bool acpi_can_fallback_to_crs(struct acpi_device *adev, const char *con_id);
136 137 138
#else
static inline void acpi_gpiochip_add(struct gpio_chip *chip) { }
static inline void acpi_gpiochip_remove(struct gpio_chip *chip) { }
139

140 141 142 143 144 145
static inline void
acpi_gpiochip_request_interrupts(struct gpio_chip *chip) { }

static inline void
acpi_gpiochip_free_interrupts(struct gpio_chip *chip) { }

146
static inline struct gpio_desc *
147 148 149
acpi_find_gpio(struct device *dev, const char *con_id,
	       unsigned int idx, enum gpiod_flags flags,
	       enum gpio_lookup_flags *lookupflags)
150
{
151
	return ERR_PTR(-ENOENT);
152
}
153 154 155 156 157 158
static inline struct gpio_desc *
acpi_node_get_gpiod(struct fwnode_handle *fwnode, const char *propname,
		    int index, struct acpi_gpio_info *info)
{
	return ERR_PTR(-ENXIO);
}
159 160 161 162
static inline int acpi_gpio_count(struct device *dev, const char *con_id)
{
	return -ENODEV;
}
163 164 165 166 167 168

static inline bool acpi_can_fallback_to_crs(struct acpi_device *adev,
					    const char *con_id)
{
	return false;
}
169 170
#endif

171
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip, u16 hwnum);
172 173 174 175
void gpiod_set_array_value_complex(bool raw, bool can_sleep,
				   unsigned int array_size,
				   struct gpio_desc **desc_array,
				   int *value_array);
176

177
extern struct spinlock gpio_lock;
178
extern struct list_head gpio_devices;
179 180

struct gpio_desc {
181
	struct gpio_device	*gdev;
182 183 184 185 186 187 188 189 190 191
	unsigned long		flags;
/* flag symbols are bit numbers */
#define FLAG_REQUESTED	0
#define FLAG_IS_OUT	1
#define FLAG_EXPORT	2	/* protected by sysfs_lock */
#define FLAG_SYSFS	3	/* exported via /sys/class/gpio/control */
#define FLAG_ACTIVE_LOW	6	/* value has active low */
#define FLAG_OPEN_DRAIN	7	/* Gpio is open drain type */
#define FLAG_OPEN_SOURCE 8	/* Gpio is open source type */
#define FLAG_USED_AS_IRQ 9	/* GPIO is connected to an IRQ */
192
#define FLAG_IS_HOGGED	11	/* GPIO is hogged */
193

194
	/* Connection label */
195
	const char		*label;
196 197
	/* Name of the GPIO */
	const char		*name;
198 199 200 201
};

int gpiod_request(struct gpio_desc *desc, const char *label);
void gpiod_free(struct gpio_desc *desc);
202 203
int gpiod_hog(struct gpio_desc *desc, const char *name,
		unsigned long lflags, enum gpiod_flags dflags);
204 205 206 207 208 209

/*
 * Return the GPIO number of the passed descriptor relative to its chip
 */
static int __maybe_unused gpio_chip_hwgpio(const struct gpio_desc *desc)
{
210
	return desc - &desc->gdev->descs[0];
211 212
}

213 214
void devprop_gpiochip_set_names(struct gpio_chip *chip);

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
/* With descriptor prefix */

#define gpiod_emerg(desc, fmt, ...)					       \
	pr_emerg("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
		 ##__VA_ARGS__)
#define gpiod_crit(desc, fmt, ...)					       \
	pr_crit("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
		 ##__VA_ARGS__)
#define gpiod_err(desc, fmt, ...)					       \
	pr_err("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",  \
		 ##__VA_ARGS__)
#define gpiod_warn(desc, fmt, ...)					       \
	pr_warn("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
		 ##__VA_ARGS__)
#define gpiod_info(desc, fmt, ...)					       \
	pr_info("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?", \
		 ##__VA_ARGS__)
#define gpiod_dbg(desc, fmt, ...)					       \
	pr_debug("gpio-%d (%s): " fmt, desc_to_gpio(desc), desc->label ? : "?",\
		 ##__VA_ARGS__)

/* With chip prefix */

#define chip_emerg(chip, fmt, ...)					\
239
	dev_emerg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
240
#define chip_crit(chip, fmt, ...)					\
241
	dev_crit(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
242
#define chip_err(chip, fmt, ...)					\
243
	dev_err(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
244
#define chip_warn(chip, fmt, ...)					\
245
	dev_warn(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
246
#define chip_info(chip, fmt, ...)					\
247
	dev_info(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
248
#define chip_dbg(chip, fmt, ...)					\
249
	dev_dbg(&chip->gpiodev->dev, "(%s): " fmt, chip->label, ##__VA_ARGS__)
250 251 252

#ifdef CONFIG_GPIO_SYSFS

253 254
int gpiochip_sysfs_register(struct gpio_device *gdev);
void gpiochip_sysfs_unregister(struct gpio_device *gdev);
255 256 257

#else

258
static inline int gpiochip_sysfs_register(struct gpio_device *gdev)
259 260 261 262
{
	return 0;
}

263
static inline void gpiochip_sysfs_unregister(struct gpio_device *gdev)
264 265 266 267 268
{
}

#endif /* CONFIG_GPIO_SYSFS */

269
#endif /* GPIOLIB_H */