diff --git a/CMakeLists.txt b/CMakeLists.txt index e5825a8..57a3c21 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,12 +3,13 @@ project(periphery C) option(BUILD_TESTS "Build test programs" ON) -# Check for Linux kernel headers +# Check for Linux kernel header files for character device GPIO support include(CheckIncludeFiles) -CHECK_INCLUDE_FILES(linux/gpio.h HAVE_LINUX_HEADERS) -if(NOT HAVE_LINUX_HEADERS) - message(FATAL_ERROR "c-periphery needs Linux kernel headers") +CHECK_INCLUDE_FILES(linux/gpio.h HAVE_CDEV_GPIO_HEADERS) +if(NOT HAVE_CDEV_GPIO_HEADERS) +message(WARNING "Linux kernel header files not found for character device GPIO support. c-periphery will be built with legacy sysfs GPIO support only.") endif() +add_definitions(-DPERIPHERY_GPIO_CDEV_SUPPORT=$) # Library version set(VERSION "2.2.1") diff --git a/Makefile b/Makefile index 86af9d9..130619b 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,8 @@ SRCS = src/gpio.c src/led.c src/pwm.c src/spi.c src/i2c.c src/mmio.c src/serial. SRCDIR = src OBJDIR = obj +PERIPHERY_GPIO_CDEV_SUPPORT ?= 1 + TEST_PROGRAMS = $(basename $(wildcard tests/*.c)) ########################################################################### @@ -14,7 +16,7 @@ COMMIT_ID := $(shell git describe --abbrev --always --tags --dirty 2>/dev/null | CFLAGS += -std=gnu99 -pedantic CFLAGS += -Wall -Wextra -Wno-unused-parameter $(DEBUG) -fPIC -CFLAGS += -DPERIPHERY_VERSION_COMMIT=\"$(COMMIT_ID)\" +CFLAGS += -DPERIPHERY_VERSION_COMMIT=\"$(COMMIT_ID)\" -DPERIPHERY_GPIO_CDEV_SUPPORT=$(PERIPHERY_GPIO_CDEV_SUPPORT) LDFLAGS += ifdef CROSS_COMPILE diff --git a/src/gpio.c b/src/gpio.c index 3d26945..ede3187 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -20,7 +20,10 @@ #include #include + +#if PERIPHERY_GPIO_CDEV_SUPPORT #include +#endif #include "gpio.h" @@ -837,6 +840,8 @@ int gpio_open_sysfs(gpio_t *gpio, unsigned int line, gpio_direction_t direction) /* cdev implementation */ /*********************************************************************************/ +#if PERIPHERY_GPIO_CDEV_SUPPORT + static int _gpio_cdev_reopen(gpio_t *gpio, gpio_direction_t direction, gpio_edge_t edge, gpio_bias_t bias, gpio_drive_t drive, bool inverted) { uint32_t flags = 0; @@ -1362,3 +1367,19 @@ int gpio_open_name(gpio_t *gpio, const char *path, const char *name, gpio_direct return gpio_open_name_advanced(gpio, path, name, &config); } + +#else /* PERIPHERY_GPIO_CDEV_SUPPORT */ + +int gpio_open_advanced(gpio_t *gpio, const char *path, unsigned int line, const gpio_config_t *config) { + return _gpio_error(gpio, GPIO_ERROR_UNSUPPORTED, 0, "c-periphery library built without character device GPIO support."); +} + +int gpio_open_name_advanced(gpio_t *gpio, const char *path, const char *name, const gpio_config_t *config) { + return _gpio_error(gpio, GPIO_ERROR_UNSUPPORTED, 0, "c-periphery library built without character device GPIO support."); +} + +int gpio_open(gpio_t *gpio, const char *path, unsigned int line, gpio_direction_t direction) { + return _gpio_error(gpio, GPIO_ERROR_UNSUPPORTED, 0, "c-periphery library built without character device GPIO support."); +} + +#endif