iPXE
gpio.h
Go to the documentation of this file.
1#ifndef _IPXE_GPIO_H
2#define _IPXE_GPIO_H
3
4/** @file
5 *
6 * General purpose I/O
7 *
8 */
9
10FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11
12#include <stdint.h>
13#include <ipxe/list.h>
14#include <ipxe/refcnt.h>
15#include <ipxe/device.h>
16
17/** A GPIO pin */
18struct gpio {
19 /** GPIO controller */
20 struct gpios *gpios;
21 /** Pin index */
22 unsigned int index;
23 /** Configuration */
24 unsigned int config;
25};
26
27/** GPIO is active low
28 *
29 * This bit is chosen to match the devicetree standard usage.
30 */
31#define GPIO_CFG_ACTIVE_LOW 0x01
32
33/** GPIO is an output */
34#define GPIO_CFG_OUTPUT 0x0100
35
36/** A GPIO controller */
37struct gpios {
38 /** Reference count */
39 struct refcnt refcnt;
40 /** List of GPIO controllers */
42 /** Generic device */
43 struct device *dev;
44 /** Number of GPIOs */
45 unsigned int count;
46
47 /** Individual GPIOs */
48 struct gpio *gpio;
49 /** GPIO operations */
51
52 /** Driver-private data */
53 void *priv;
54};
55
56/** GPIO operations */
58 /**
59 * Get current GPIO input value
60 *
61 * @v gpios GPIO controller
62 * @v gpio GPIO pin
63 * @ret active Pin is in the active state
64 */
65 int ( * in ) ( struct gpios *gpios, struct gpio *gpio );
66 /**
67 * Set current GPIO output value
68 *
69 * @v gpios GPIO controller
70 * @v gpio GPIO pin
71 * @v active Set pin to active state
72 */
73 void ( * out ) ( struct gpios *gpios, struct gpio *gpio, int active );
74 /**
75 * Configure GPIO pin
76 *
77 * @v gpios GPIO controller
78 * @v gpio GPIO pin
79 * @v config Configuration
80 * @ret rc Return status code
81 */
82 int ( * config ) ( struct gpios *gpios, struct gpio *gpio,
83 unsigned int config );
84};
85
87
88/**
89 * Get reference to GPIO controller
90 *
91 * @v gpios GPIO controller
92 * @ret gpios GPIO controller
93 */
94static inline __attribute__ (( always_inline )) struct gpios *
95gpios_get ( struct gpios *gpios ) {
96 ref_get ( &gpios->refcnt );
97 return gpios;
98}
99
100/**
101 * Drop reference to GPIO controller
102 *
103 * @v gpios GPIO controller
104 */
105static inline __attribute__ (( always_inline )) void
106gpios_put ( struct gpios *gpios ) {
107 ref_put ( &gpios->refcnt );
108}
109
110/**
111 * Get reference to GPIO pin
112 *
113 * @v gpio GPIO pin
114 * @ret gpio GPIO pin
115 */
116static inline __attribute__ (( always_inline )) struct gpio *
117gpio_get ( struct gpio *gpio ) {
118 gpios_get ( gpio->gpios );
119 return gpio;
120}
121
122/**
123 * Drop reference to GPIO ping
124 *
125 * @v gpio GPIO pin
126 */
127static inline __attribute__ (( always_inline )) void
128gpio_put ( struct gpio *gpio ) {
129 gpios_put ( gpio->gpios );
130}
131
132/**
133 * Initialise a GPIO controller
134 *
135 * @v gpios GPIO controller
136 * @v op GPIO operations
137 */
138static inline __attribute__ (( always_inline )) void
140 gpios->op = op;
141}
142
143/**
144 * Stop using a GPIO controller
145 *
146 * @v gpios GPIO controller
147 *
148 * Drivers should call this method immediately before the final call
149 * to gpios_put().
150 */
151static inline __attribute__ (( always_inline )) void
155
156/**
157 * Get current GPIO input value
158 *
159 * @v gpio GPIO pin
160 * @ret active Pin is in the active state
161 */
162static inline int gpio_in ( struct gpio *gpio ) {
163 struct gpios *gpios = gpio->gpios;
164
165 return gpios->op->in ( gpios, gpio );
166}
167
168/**
169 * Set current GPIO output value
170 *
171 * @v gpio GPIO pin
172 * @v active Set pin to active state
173 */
174static inline void gpio_out ( struct gpio *gpio, int active ) {
175 struct gpios *gpios = gpio->gpios;
176
177 gpios->op->out ( gpios, gpio, active );
178}
179
180/**
181 * Configure GPIO pin
182 *
183 * @v gpio GPIO pin
184 * @v config Configuration
185 * @ret rc Return status code
186 */
187static inline int gpio_config ( struct gpio *gpio, unsigned int config ) {
188 struct gpios *gpios = gpio->gpios;
189
190 return gpios->op->config ( gpios, gpio, config );
191}
192
193extern struct gpios * alloc_gpios ( unsigned int count, size_t priv_len );
194extern int gpios_register ( struct gpios *gpios );
195extern void gpios_unregister ( struct gpios *gpios );
196extern struct gpios * gpios_find ( unsigned int bus_type,
197 unsigned int location );
198
199#endif /* _IPXE_GPIO_H */
Device model.
struct gpio_operations null_gpio_operations
Null GPIO operations.
Definition gpio.c:161
static int gpio_config(struct gpio *gpio, unsigned int config)
Configure GPIO pin.
Definition gpio.h:187
static void gpios_nullify(struct gpios *gpios)
Stop using a GPIO controller.
Definition gpio.h:152
int gpios_register(struct gpios *gpios)
Register GPIO controller.
Definition gpio.c:78
struct gpios * alloc_gpios(unsigned int count, size_t priv_len)
Allocate GPIO controller.
Definition gpio.c:46
static struct gpios * gpios_get(struct gpios *gpios)
Get reference to GPIO controller.
Definition gpio.h:95
static void gpio_put(struct gpio *gpio)
Drop reference to GPIO ping.
Definition gpio.h:128
static struct gpio * gpio_get(struct gpio *gpio)
Get reference to GPIO pin.
Definition gpio.h:117
struct gpios * gpios_find(unsigned int bus_type, unsigned int location)
Find GPIO controller.
Definition gpio.c:109
static void gpio_out(struct gpio *gpio, int active)
Set current GPIO output value.
Definition gpio.h:174
static int gpio_in(struct gpio *gpio)
Get current GPIO input value.
Definition gpio.h:162
static void gpios_init(struct gpios *gpios, struct gpio_operations *op)
Initialise a GPIO controller.
Definition gpio.h:139
static void gpios_put(struct gpios *gpios)
Drop reference to GPIO controller.
Definition gpio.h:106
void gpios_unregister(struct gpios *gpios)
Unregister GPIO controller.
Definition gpio.c:94
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define __attribute__(x)
Definition compiler.h:10
Linked lists.
static uint16_t struct vmbus_xfer_pages_operations * op
Definition netvsc.h:327
Reference counting.
#define ref_get(refcnt)
Get additional reference to object.
Definition refcnt.h:93
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107
A hardware device.
Definition device.h:77
GPIO operations.
Definition gpio.h:57
int(* in)(struct gpios *gpios, struct gpio *gpio)
Get current GPIO input value.
Definition gpio.h:65
void(* out)(struct gpios *gpios, struct gpio *gpio, int active)
Set current GPIO output value.
Definition gpio.h:73
int(* config)(struct gpios *gpios, struct gpio *gpio, unsigned int config)
Configure GPIO pin.
Definition gpio.h:82
A GPIO pin.
Definition gpio.h:18
unsigned int config
Configuration.
Definition gpio.h:24
struct gpios * gpios
GPIO controller.
Definition gpio.h:20
unsigned int index
Pin index.
Definition gpio.h:22
A GPIO controller.
Definition gpio.h:37
void * priv
Driver-private data.
Definition gpio.h:53
unsigned int count
Number of GPIOs.
Definition gpio.h:45
struct gpio_operations * op
GPIO operations.
Definition gpio.h:50
struct device * dev
Generic device.
Definition gpio.h:43
struct list_head list
List of GPIO controllers.
Definition gpio.h:41
struct gpio * gpio
Individual GPIOs.
Definition gpio.h:48
struct refcnt refcnt
Reference count.
Definition gpio.h:39
A doubly-linked list entry (or list head)
Definition list.h:19