Skip to content

Commit c15aa9a

Browse files
committed
pinctrl: Create and use gpiolib library
Create the gpiolib library to contain all of the gpiolib code and the gpiochips, so that applications such as pinctrl are just linked against it, and not all of the individual object files. The named section approach to collecting the gpiochips does not work in this situation, so add an explicit array of pointers to the gpiochip implementations. Signed-off-by: Phil Elwell <[email protected]>
1 parent 92900c5 commit c15aa9a

File tree

7 files changed

+49
-10
lines changed

7 files changed

+49
-10
lines changed

pinctrl/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror -pedantic")
66
#set project name
77
project(pinctrl)
88

9+
add_compile_definitions(LIBRARY_BUILD=1)
10+
11+
add_library(gpiolib STATIC gpiolib.c util.c library_gpiochips.c gpiochip_bcm2835.c gpiochip_bcm2712.c gpiochip_rp1.c)
12+
913
#add executables
10-
add_executable(pinctrl pinctrl.c gpiolib.c util.c gpiochip_bcm2835.c gpiochip_bcm2712.c gpiochip_rp1.c)
14+
add_executable(pinctrl pinctrl.c)
15+
target_link_libraries(pinctrl gpiolib)
1116
install(TARGETS pinctrl RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
17+
install(TARGETS gpiolib ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
1218
install(FILES pinctrl-completion.bash RENAME pinctrl DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/bash-completion/completions")

pinctrl/gpiochip.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33

44
#include "gpiolib.h"
55

6+
#if LIBRARY_BUILD
7+
#define DECLARE_GPIO_CHIP(name, compatible, iface, size, data) \
8+
const GPIO_CHIP_T name ## _chip = { #name, compatible, iface, size, data }
9+
#else
610
#define DECLARE_GPIO_CHIP(name, compatible, iface, size, data) \
711
const GPIO_CHIP_T name ## _chip = { #name, compatible, iface, size, data }; \
812
const GPIO_CHIP_T *name ## _chip_ptr __attribute__ ((section ("gpiochips"))) __attribute__ ((used)) = &(name ## _chip)
13+
#endif
914

1015
typedef struct GPIO_CHIP_INTERFACE_ GPIO_CHIP_INTERFACE_T;
1116

@@ -36,7 +41,12 @@ struct GPIO_CHIP_INTERFACE_
3641
const char * (*gpio_get_fsel_name)(void *priv, uint32_t gpio, GPIO_FSEL_T fsel);
3742
};
3843

44+
#if LIBRARY_BUILD
45+
extern const GPIO_CHIP_T *const library_gpiochips[];
46+
extern const int library_gpiochips_count;
47+
#else
3948
extern const GPIO_CHIP_T *__start_gpiochips;
4049
extern const GPIO_CHIP_T *__stop_gpiochips;
50+
#endif
4151

4252
#endif

pinctrl/gpiochip_bcm2712.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
#include "gpiochip.h"
1010
#include "util.h"
1111

12-
#define ARRAY_SIZE(_a) (sizeof(_a)/sizeof(_a[0]))
13-
1412
/* 2712 definitions */
1513

1614
#define BCM2712_GIO_DATA 0x04

pinctrl/gpiochip_bcm2835.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include "gpiochip.h"
99
#include "util.h"
1010

11-
#define ARRAY_SIZE(_a) (sizeof(_a)/sizeof(_a[0]))
12-
1311
#define BCM2835_NUM_GPIOS 54
1412
#define BCM2835_ALT_COUNT 6
1513

pinctrl/gpiolib.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include "gpiochip.h"
1313
#include "util.h"
1414

15-
#define ARRAY_SIZE(_a) (sizeof(_a)/sizeof(_a[0]))
16-
1715
#define MAX_GPIO_CHIPS 8
1816

1917
typedef struct GPIO_CHIP_INSTANCE_
@@ -394,9 +392,16 @@ const char *gpio_get_drive_name(GPIO_DRIVE_T drive)
394392

395393
static const GPIO_CHIP_T *gpio_find_chip(const char *name)
396394
{
397-
const GPIO_CHIP_T **chip_ptr;
398-
399-
for (chip_ptr = &__start_gpiochips; name && chip_ptr < &__stop_gpiochips; chip_ptr++) {
395+
#if LIBRARY_BUILD
396+
const GPIO_CHIP_T *const *start = &library_gpiochips[0];
397+
const GPIO_CHIP_T *const *end = &library_gpiochips[0] + library_gpiochips_count;
398+
#else
399+
const GPIO_CHIP_T *const *start = &__start_gpiochips;
400+
const GPIO_CHIP_T *const *end = &__stop_gpiochips;
401+
#endif
402+
const GPIO_CHIP_T *const *chip_ptr;
403+
404+
for (chip_ptr = start; name && chip_ptr < end; chip_ptr++) {
400405
const GPIO_CHIP_T *chip = *chip_ptr;
401406
if (!strcmp(name, chip->name) ||
402407
!strcmp(name, chip->compatible))

pinctrl/library_gpiochips.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include "gpiochip.h"
2+
#include "util.h"
3+
4+
#define GPIO_CHIP(name) name ## _chip
5+
#define EXTERN_GPIO_CHIP(name) extern const GPIO_CHIP_T GPIO_CHIP(name)
6+
7+
EXTERN_GPIO_CHIP(bcm2835);
8+
EXTERN_GPIO_CHIP(bcm2711);
9+
EXTERN_GPIO_CHIP(bcm2712);
10+
EXTERN_GPIO_CHIP(rp1);
11+
12+
const GPIO_CHIP_T *const library_gpiochips[] =
13+
{
14+
&GPIO_CHIP(bcm2835),
15+
&GPIO_CHIP(bcm2711),
16+
&GPIO_CHIP(bcm2712),
17+
&GPIO_CHIP(rp1),
18+
};
19+
20+
const int library_gpiochips_count = ARRAY_SIZE(library_gpiochips);

pinctrl/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#ifndef _UTIL_H
22
#define _UTIL_H
33

4+
#include <stddef.h>
45
#include <stdint.h>
56

67
#define INVALID_ADDRESS ((uint64_t)~0)
78
#define ROUND_UP(n, d) ((((n) + (d) - 1) / (d)) * (d))
89
#define UNUSED(x) (void)(x)
10+
#define ARRAY_SIZE(_a) (sizeof(_a)/sizeof(_a[0]))
911

1012
typedef struct dt_subnode_iter *DT_SUBNODE_HANDLE;
1113
char *read_text_file(const char *fname, size_t *plen);

0 commit comments

Comments
 (0)