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 
10 FILE_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 */
18 struct 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 */
37 struct gpios {
38  /** Reference count */
39  struct refcnt refcnt;
40  /** List of GPIO controllers */
41  struct list_head list;
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  */
94 static inline __attribute__ (( always_inline )) struct gpios *
95 gpios_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  */
105 static inline __attribute__ (( always_inline )) void
106 gpios_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  */
116 static inline __attribute__ (( always_inline )) struct gpio *
117 gpio_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  */
127 static inline __attribute__ (( always_inline )) void
128 gpio_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  */
138 static inline __attribute__ (( always_inline )) void
139 gpios_init ( struct gpios *gpios, struct gpio_operations *op ) {
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  */
151 static inline __attribute__ (( always_inline )) void
152 gpios_nullify ( struct gpios *gpios ) {
154 }
155 
156 /**
157  * Get current GPIO input value
158  *
159  * @v gpio GPIO pin
160  * @ret active Pin is in the active state
161  */
162 static 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  */
174 static 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  */
187 static 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 
193 extern struct gpios * alloc_gpios ( unsigned int count, size_t priv_len );
194 extern int gpios_register ( struct gpios *gpios );
195 extern void gpios_unregister ( struct gpios *gpios );
196 extern struct gpios * gpios_find ( unsigned int bus_type,
197  unsigned int location );
198 
199 #endif /* _IPXE_GPIO_H */
#define __attribute__(x)
Definition: compiler.h:10
struct gpios * alloc_gpios(unsigned int count, size_t priv_len)
Allocate GPIO controller.
Definition: gpio.c:46
struct gpios * gpios
GPIO controller.
Definition: gpio.h:20
static void gpios_put(struct gpios *gpios)
Drop reference to GPIO controller.
Definition: gpio.h:106
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned int index
Pin index.
Definition: gpio.h:22
struct device * dev
Generic device.
Definition: gpio.h:43
void gpios_unregister(struct gpios *gpios)
Unregister GPIO controller.
Definition: gpio.c:94
struct gpio_operations * op
GPIO operations.
Definition: gpio.h:50
A doubly-linked list entry (or list head)
Definition: list.h:18
A reference counter.
Definition: refcnt.h:26
pseudo_bit_t gpio[0x00001]
Definition: arbel.h:30
static void gpio_out(struct gpio *gpio, int active)
Set current GPIO output value.
Definition: gpio.h:174
A hardware device.
Definition: device.h:76
int(* in)(struct gpios *gpios, struct gpio *gpio)
Get current GPIO input value.
Definition: gpio.h:65
int gpios_register(struct gpios *gpios)
Register GPIO controller.
Definition: gpio.c:78
static unsigned int count
Number of entries.
Definition: dwmac.h:225
GPIO operations.
Definition: gpio.h:57
struct gpios * gpios_find(unsigned int bus_type, unsigned int location)
Find GPIO controller.
Definition: gpio.c:109
Linked lists.
unsigned int config
Configuration.
Definition: gpio.h:24
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:92
struct list_head list
List of GPIO controllers.
Definition: gpio.h:41
static struct gpio * gpio_get(struct gpio *gpio)
Get reference to GPIO pin.
Definition: gpio.h:117
A GPIO controller.
Definition: gpio.h:37
struct refcnt refcnt
Reference count.
Definition: gpio.h:39
static void gpios_init(struct gpios *gpios, struct gpio_operations *op)
Initialise a GPIO controller.
Definition: gpio.h:139
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
static void gpios_nullify(struct gpios *gpios)
Stop using a GPIO controller.
Definition: gpio.h:152
static int gpio_config(struct gpio *gpio, unsigned int config)
Configure GPIO pin.
Definition: gpio.h:187
Reference counting.
static int gpio_in(struct gpio *gpio)
Get current GPIO input value.
Definition: gpio.h:162
Device model.
A GPIO pin.
Definition: gpio.h:18
struct gpio_operations null_gpio_operations
Null GPIO operations.
Definition: gpio.c:161
int(* config)(struct gpios *gpios, struct gpio *gpio, unsigned int config)
Configure GPIO pin.
Definition: gpio.h:82
unsigned int count
Number of GPIOs.
Definition: gpio.h:45
static void gpio_put(struct gpio *gpio)
Drop reference to GPIO ping.
Definition: gpio.h:128
static struct gpios * gpios_get(struct gpios *gpios)
Get reference to GPIO controller.
Definition: gpio.h:95
void(* out)(struct gpios *gpios, struct gpio *gpio, int active)
Set current GPIO output value.
Definition: gpio.h:73
struct gpio * gpio
Individual GPIOs.
Definition: gpio.h:48
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
void * priv
Driver-private data.
Definition: gpio.h:53