Skip to content

Commit

Permalink
Add local gpio control
Browse files Browse the repository at this point in the history
This driver permits controlling local (on the system) gpios
by using the libgpiod library

Signed-off-by: Neil Armstrong <[email protected]>
  • Loading branch information
superna9999 committed Oct 23, 2023
1 parent fbdc677 commit e81f439
Show file tree
Hide file tree
Showing 13 changed files with 501 additions and 0 deletions.
1 change: 1 addition & 0 deletions ci/archlinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pacman -Syu --noconfirm \
libftdi-compat \
libyaml \
systemd-libs \
libgpiod \
pkgconf \
meson \
$PKGS_CC
Expand Down
1 change: 1 addition & 0 deletions ci/debian.cross-compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ apt install -y --no-install-recommends \
libftdi-dev:${ARCH} \
libudev-dev:${ARCH} \
libyaml-dev:${ARCH} \
libgpiod-dev:${ARCH} \
gcc-`dpkg-architecture -a ${ARCH} -q DEB_TARGET_GNU_TYPE`

echo "Install finished: $0"
1 change: 1 addition & 0 deletions ci/debian.i386.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ apt install -y --no-install-recommends \
libftdi-dev:i386 \
libudev-dev:i386 \
libyaml-dev:i386 \
libgpiod-dev:i386 \
$PKGS_CC

echo "Install finished: $0"
1 change: 1 addition & 0 deletions ci/debian.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ apt install -y --no-install-recommends \
libftdi-dev \
libudev-dev \
libyaml-dev \
libgpiod-dev \
meson \
$PKGS_CC

Expand Down
1 change: 1 addition & 0 deletions ci/fedora.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ dnf -y install \
libftdi-devel \
libudev-devel \
libyaml-devel \
libgpiod-devel \
meson \
$PKGS_CC

Expand Down
7 changes: 7 additions & 0 deletions config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef __CDBA_CONFIG_H__
#define __CDBA_CONFIG_H__

/* libgpio major version */
#define GPIOD_MAJOR_VERSION @gpiod_major_version@

#endif
1 change: 1 addition & 0 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ extern const struct control_ops alpaca_ops;
extern const struct control_ops cdb_assist_ops;
extern const struct control_ops conmux_ops;
extern const struct control_ops ftdi_gpio_ops;
extern const struct control_ops local_gpio_ops;
extern const struct control_ops external_ops;
extern const struct control_ops qcomlt_dbg_ops;

Expand Down
3 changes: 3 additions & 0 deletions device_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ static void parse_board(struct device_parser *dp)
} else if (!strcmp(key, "ftdi_gpio")) {
dev->control_dev = strdup(value);
set_control_ops(dev, &ftdi_gpio_ops);
} else if (!strcmp(key, "local_gpio")) {
dev->control_dev = strdup(value);
set_control_ops(dev, &local_gpio_ops);
} else if (!strcmp(key, "external")) {
dev->control_dev = strdup(value);
set_control_ops(dev, &external_ops);
Expand Down
90 changes: 90 additions & 0 deletions local-gpio-v1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2023, Linaro Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>

/* local-gpio implementation for libgpiod major version 1 */

#include "local-gpio.h"

#include <gpiod.h>

int local_gpio_init(struct local_gpio *local_gpio)
{
int i;

local_gpio->chip = gpiod_chip_open_lookup(local_gpio->gpiochip_desc);
if (!local_gpio->chip) {
err(1, "Unable to open gpiochip '%s'", local_gpio->gpiochip_desc);
return -1;
}

for (i = 0; i < GPIO_COUNT; ++i) {
struct gpiod_line_request_config cfg;

if (!local_gpio->gpio_present[i])
continue;

cfg.consumer = "cdba";
cfg.request_type = GPIOD_LINE_REQUEST_DIRECTION_OUTPUT;
cfg.flags = 0;

if (local_gpio->gpio_polarity[i])
cfg.flags = GPIOD_LINE_REQUEST_FLAG_ACTIVE_LOW;

local_gpio->gpio_line[i] = gpiod_chip_get_line(local_gpio->chip,
local_gpio->gpio_offset[i]);

if (!local_gpio->gpio_line[i]) {
err(1, "Unable to find gpio %d offset %u", i, local_gpio->gpio_offset[i]);
return -1;
}

if (gpiod_line_request(local_gpio->gpio_line[i], &cfg, 0)) {
err(1, "Unable to request gpio %d offset %u", i, local_gpio->gpio_offset[i]);
return -1;
}
}

return 0;
}

int local_gpio_set_value(struct local_gpio *local_gpio, unsigned int gpio, bool on)
{
return gpiod_line_set_value(local_gpio->gpio_line[gpio], on);
}
125 changes: 125 additions & 0 deletions local-gpio-v2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2023, Linaro Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>

/* local-gpio implementation for libgpiod major version 2 */

#include "local-gpio.h"

#include <gpiod.h>

int local_gpio_init(struct local_gpio *local_gpio)
{
struct gpiod_request_config *req_cfg;
char *gpiochip_path;
int i;

if (asprintf(&gpiochip_path, "/dev/%s", local_gpio->gpiochip_desc) < 0) {
free(local_gpio);
return -1;
}

local_gpio->chip = gpiod_chip_open(gpiochip_path);
if (!local_gpio->chip) {
err(1, "Unable to open gpiochip '%s'", local_gpio->gpiochip_desc);
return -1;
}

req_cfg = gpiod_request_config_new();
if (!req_cfg) {
err(1, "Unable to allocate request config");
return -1;
}
gpiod_request_config_set_consumer(req_cfg, "cdba");

for (i = 0; i < GPIO_COUNT; ++i) {
struct gpiod_line_settings *line_settings;
struct gpiod_line_config *line_cfg;

if (!local_gpio->gpio_present[i])
continue;

line_settings = gpiod_line_settings_new();
if (!line_settings) {
err(1, "Unable to allocate gpio line settings");
return -1;
}
if (gpiod_line_settings_set_direction(line_settings,
GPIOD_LINE_DIRECTION_OUTPUT) < 0) {
err(1, "Unable to set line direction");
return -1;
}
if (local_gpio->gpio_polarity[i]) {
gpiod_line_settings_set_active_low(line_settings, true);
}
if (gpiod_line_settings_set_output_value(line_settings,
GPIOD_LINE_VALUE_INACTIVE) < 0) {
err(1, "Unable to set line output value");
return -1;
}

line_cfg = gpiod_line_config_new();
if (!line_cfg) {
err(1, "Unable to allocate gpio line settings");
return -1;
}
if (gpiod_line_config_add_line_settings(line_cfg, &local_gpio->gpio_offset[i], 1,
line_settings) < 0) {
err(1, "Unable to set line config");
return -1;
}

local_gpio->gpio_line[i] = gpiod_chip_request_lines(local_gpio->chip,
req_cfg, line_cfg);

if (!local_gpio->gpio_line[i]) {
err(1, "Unable to request gpio %d offset %u", i, local_gpio->gpio_offset[i]);
return -1;
}
}

return 0;
}

int local_gpio_set_value(struct local_gpio *local_gpio, unsigned int gpio, bool on)
{
return gpiod_line_request_set_value(local_gpio->gpio_line[gpio], 0,
on ? GPIOD_LINE_VALUE_ACTIVE
: GPIOD_LINE_VALUE_INACTIVE);
}
Loading

0 comments on commit e81f439

Please sign in to comment.