iPXE
gpio.h File Reference

General purpose I/O. More...

#include <stdint.h>
#include <ipxe/list.h>
#include <ipxe/refcnt.h>
#include <ipxe/device.h>

Go to the source code of this file.

Data Structures

struct  gpio
 A GPIO pin. More...
struct  gpios
 A GPIO controller. More...
struct  gpio_operations
 GPIO operations. More...

Macros

#define GPIO_CFG_ACTIVE_LOW   0x01
 GPIO is active low.
#define GPIO_CFG_OUTPUT   0x0100
 GPIO is an output.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static struct gpiosgpios_get (struct gpios *gpios)
 Get reference to GPIO controller.
static void gpios_put (struct gpios *gpios)
 Drop reference to GPIO controller.
static struct gpiogpio_get (struct gpio *gpio)
 Get reference to GPIO pin.
static void gpio_put (struct gpio *gpio)
 Drop reference to GPIO ping.
static void gpios_init (struct gpios *gpios, struct gpio_operations *op)
 Initialise a GPIO controller.
static void gpios_nullify (struct gpios *gpios)
 Stop using a GPIO controller.
static int gpio_in (struct gpio *gpio)
 Get current GPIO input value.
static void gpio_out (struct gpio *gpio, int active)
 Set current GPIO output value.
static int gpio_config (struct gpio *gpio, unsigned int config)
 Configure GPIO pin.
struct gpiosalloc_gpios (unsigned int count, size_t priv_len)
 Allocate GPIO controller.
int gpios_register (struct gpios *gpios)
 Register GPIO controller.
void gpios_unregister (struct gpios *gpios)
 Unregister GPIO controller.
struct gpiosgpios_find (unsigned int bus_type, unsigned int location)
 Find GPIO controller.

Variables

struct gpio_operations null_gpio_operations
 Null GPIO operations.

Detailed Description

General purpose I/O.

Definition in file gpio.h.

Macro Definition Documentation

◆ GPIO_CFG_ACTIVE_LOW

#define GPIO_CFG_ACTIVE_LOW   0x01

GPIO is active low.

This bit is chosen to match the devicetree standard usage.

Definition at line 31 of file gpio.h.

◆ GPIO_CFG_OUTPUT

#define GPIO_CFG_OUTPUT   0x0100

GPIO is an output.

Definition at line 34 of file gpio.h.

Referenced by dwgpio_config().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ gpios_get()

struct gpios * gpios_get ( struct gpios * gpios)
inlinestatic

Get reference to GPIO controller.

Parameters
gpiosGPIO controller
Return values
gpiosGPIO controller

Definition at line 95 of file gpio.h.

95 {
96 ref_get ( &gpios->refcnt );
97 return gpios;
98}
#define ref_get(refcnt)
Get additional reference to object.
Definition refcnt.h:93
A GPIO controller.
Definition gpio.h:37
struct refcnt refcnt
Reference count.
Definition gpio.h:39

References ref_get, and gpios::refcnt.

Referenced by gpio_get(), and gpios_register().

◆ gpios_put()

void gpios_put ( struct gpios * gpios)
inlinestatic

Drop reference to GPIO controller.

Parameters
gpiosGPIO controller

Definition at line 106 of file gpio.h.

106 {
107 ref_put ( &gpios->refcnt );
108}
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107

References ref_put, and gpios::refcnt.

Referenced by dwgpio_probe(), dwgpio_remove(), gpio_put(), and gpios_unregister().

◆ gpio_get()

struct gpio * gpio_get ( struct gpio * gpio)
inlinestatic

Get reference to GPIO pin.

Parameters
gpioGPIO pin
Return values
gpioGPIO pin

Definition at line 117 of file gpio.h.

117 {
118 gpios_get ( gpio->gpios );
119 return gpio;
120}
static struct gpios * gpios_get(struct gpios *gpios)
Get reference to GPIO controller.
Definition gpio.h:95
A GPIO pin.
Definition gpio.h:18
struct gpios * gpios
GPIO controller.
Definition gpio.h:20

References gpio::gpios, and gpios_get().

◆ gpio_put()

void gpio_put ( struct gpio * gpio)
inlinestatic

Drop reference to GPIO ping.

Parameters
gpioGPIO pin

Definition at line 128 of file gpio.h.

128 {
129 gpios_put ( gpio->gpios );
130}
static void gpios_put(struct gpios *gpios)
Drop reference to GPIO controller.
Definition gpio.h:106

References gpio::gpios, and gpios_put().

◆ gpios_init()

void gpios_init ( struct gpios * gpios,
struct gpio_operations * op )
inlinestatic

Initialise a GPIO controller.

Parameters
gpiosGPIO controller
opGPIO operations

Definition at line 139 of file gpio.h.

139 {
140 gpios->op = op;
141}
static uint16_t struct vmbus_xfer_pages_operations * op
Definition netvsc.h:327
struct gpio_operations * op
GPIO operations.
Definition gpio.h:50

References gpios::op, and op.

Referenced by dwgpio_probe().

◆ gpios_nullify()

void gpios_nullify ( struct gpios * gpios)
inlinestatic

Stop using a GPIO controller.

Parameters
gpiosGPIO controller

Drivers should call this method immediately before the final call to gpios_put().

Definition at line 152 of file gpio.h.

152 {
154}
struct gpio_operations null_gpio_operations
Null GPIO operations.
Definition gpio.c:161

References null_gpio_operations, and gpios::op.

Referenced by dwgpio_probe(), and dwgpio_remove().

◆ gpio_in()

int gpio_in ( struct gpio * gpio)
inlinestatic

Get current GPIO input value.

Parameters
gpioGPIO pin
Return values
activePin is in the active state

Definition at line 162 of file gpio.h.

162 {
163 struct gpios *gpios = gpio->gpios;
164
165 return gpios->op->in ( gpios, gpio );
166}
int(* in)(struct gpios *gpios, struct gpio *gpio)
Get current GPIO input value.
Definition gpio.h:65

References gpio::gpios, gpio_operations::in, and gpios::op.

◆ gpio_out()

void gpio_out ( struct gpio * gpio,
int active )
inlinestatic

Set current GPIO output value.

Parameters
gpioGPIO pin
activeSet pin to active state

Definition at line 174 of file gpio.h.

174 {
175 struct gpios *gpios = gpio->gpios;
176
177 gpios->op->out ( gpios, gpio, active );
178}
void(* out)(struct gpios *gpios, struct gpio *gpio, int active)
Set current GPIO output value.
Definition gpio.h:73

References gpio::gpios, gpios::op, and gpio_operations::out.

◆ gpio_config()

int gpio_config ( struct gpio * gpio,
unsigned int config )
inlinestatic

Configure GPIO pin.

Parameters
gpioGPIO pin
configConfiguration
Return values
rcReturn status code

Definition at line 187 of file gpio.h.

187 {
188 struct gpios *gpios = gpio->gpios;
189
190 return gpios->op->config ( gpios, gpio, config );
191}
int(* config)(struct gpios *gpios, struct gpio *gpio, unsigned int config)
Configure GPIO pin.
Definition gpio.h:82

References gpio_operations::config, gpio::gpios, and gpios::op.

◆ alloc_gpios()

struct gpios * alloc_gpios ( unsigned int count,
size_t priv_len )
extern

Allocate GPIO controller.

Parameters
countNumber of GPIO pins
priv_lenSize of driver-private data
Return values
gpiosGPIO controller, or NULL

Definition at line 46 of file gpio.c.

46 {
47 struct gpios *gpios;
48 struct gpio *gpio;
49 size_t len;
50 unsigned int i;
51
52 /* Allocate and initialise structure */
53 len = ( sizeof ( *gpios ) + ( count * sizeof ( *gpio ) ) + priv_len );
54 gpios = zalloc ( len );
55 if ( ! gpios )
56 return NULL;
57 gpios->count = count;
58 gpios->gpio = ( ( ( void * ) gpios ) + sizeof ( *gpios ) );
59 gpios->priv = ( ( ( void * ) gpios ) + sizeof ( *gpios ) +
60 ( count * sizeof ( *gpio ) ) );
61
62 /* Initialise GPIO pins */
63 for ( i = 0 ; i < count ; i++ ) {
64 gpio = &gpios->gpio[i];
65 gpio->gpios = gpios;
66 gpio->index = i;
67 }
68
69 return gpios;
70}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
ring len
Length.
Definition dwmac.h:226
static unsigned int count
Number of entries.
Definition dwmac.h:220
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
unsigned int index
Pin index.
Definition gpio.h:22
void * priv
Driver-private data.
Definition gpio.h:53
unsigned int count
Number of GPIOs.
Definition gpio.h:45
struct gpio * gpio
Individual GPIOs.
Definition gpio.h:48

References count, gpios::count, gpios::gpio, gpio::gpios, gpio::index, len, NULL, gpios::priv, and zalloc().

Referenced by dwgpio_probe().

◆ gpios_register()

int gpios_register ( struct gpios * gpios)
extern

Register GPIO controller.

Parameters
gpiosGPIO controller
Return values
rcReturn status code

Definition at line 78 of file gpio.c.

78 {
79
80 /* Add to list of GPIO controllers */
81 gpios_get ( gpios );
82 list_add_tail ( &gpios->list, &all_gpios );
83 DBGC ( gpios, "GPIO %s registered with %d GPIOs\n",
84 gpios->dev->name, gpios->count );
85
86 return 0;
87}
#define DBGC(...)
Definition compiler.h:505
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition list.h:94
char name[40]
Name.
Definition device.h:79
struct device * dev
Generic device.
Definition gpio.h:43
struct list_head list
List of GPIO controllers.
Definition gpio.h:41

References gpios::count, DBGC, gpios::dev, gpios_get(), gpios::list, list_add_tail, and device::name.

Referenced by dwgpio_probe().

◆ gpios_unregister()

void gpios_unregister ( struct gpios * gpios)
extern

Unregister GPIO controller.

Parameters
gpiosGPIO controller

Definition at line 94 of file gpio.c.

94 {
95
96 /* Remove from list of GPIO controllers */
97 DBGC ( gpios, "GPIO %s unregistered\n", gpios->dev->name );
98 list_del ( &gpios->list );
99 gpios_put ( gpios );
100}
#define list_del(list)
Delete an entry from a list.
Definition list.h:120

References DBGC, gpios::dev, gpios_put(), gpios::list, list_del, and device::name.

Referenced by dwgpio_probe(), and dwgpio_remove().

◆ gpios_find()

struct gpios * gpios_find ( unsigned int bus_type,
unsigned int location )
extern

Find GPIO controller.

Parameters
bus_typeBus type
locationBus location
Return values
gpiosGPIO controller, or NULL

Definition at line 109 of file gpio.c.

109 {
110 struct gpios *gpios;
111
112 /* Scan through list of registered GPIO controllers */
113 list_for_each_entry ( gpios, &all_gpios, list ) {
114 if ( ( gpios->dev->desc.bus_type == bus_type ) &&
115 ( gpios->dev->desc.location == location ) )
116 return gpios;
117 }
118
119 return NULL;
120}
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432
unsigned int bus_type
Bus type.
Definition device.h:25
unsigned int location
Location.
Definition device.h:30
struct device_description desc
Device description.
Definition device.h:83

References device_description::bus_type, device::desc, gpios::dev, gpios::list, list_for_each_entry, device_description::location, and NULL.

Variable Documentation

◆ null_gpio_operations

struct gpio_operations null_gpio_operations
extern

Null GPIO operations.

Definition at line 161 of file gpio.c.

161 {
162 .in = null_gpio_in,
163 .out = null_gpio_out,
164 .config = null_gpio_config,
165};
static int null_gpio_config(struct gpios *gpios __unused, struct gpio *gpio __unused, unsigned int config __unused)
Configure null GPIO pin.
Definition gpio.c:154
static int null_gpio_in(struct gpios *gpios __unused, struct gpio *gpio __unused)
Get null GPIO input value.
Definition gpio.c:129
static void null_gpio_out(struct gpios *gpios __unused, struct gpio *gpio __unused, int active __unused)
Set null GPIO output value.
Definition gpio.c:141

Referenced by gpios_nullify().