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.

Support for gpiod v1 and v2 is added, and parses the options
via the yaml parser.

This can be tested with the gpio-sim module:
https://docs.kernel.org/admin-guide/gpio/gpio-sim.html

Signed-off-by: Neil Armstrong <[email protected]>
  • Loading branch information
superna9999 committed Oct 30, 2023
1 parent 55902b7 commit 87fbc38
Show file tree
Hide file tree
Showing 13 changed files with 522 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,26 @@ devices:
fastboot: 91671140
fastboot_set_active: true
fastboot_key_timeout: 2

- board: testboard
console: /dev/serial/by-id/usb-1234-if00-port0
name: GPIO controller board
local_gpio:
- power:
chip: gpiochip0
line: 7
active_low: true
- fastboot_key:
chip: gpiochip0
line: 8
active_low: true
- power_key:
chip: gpiochip0
line: 14
active_low: true
- usb_disconnect:
chip: gpiochip1
line: 4
fastboot: cacafada
fastboot_set_active: true
fastboot_key_timeout: 2
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
1 change: 1 addition & 0 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,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
7 changes: 7 additions & 0 deletions device_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ static void parse_board(struct device_parser *dp)
continue;
}

if (!strcmp(key, "local_gpio")) {
dev->control_options = local_gpio_ops.parse_options(dp);
if (dev->control_options)
set_control_ops(dev, &local_gpio_ops);
continue;
}

device_parser_expect(dp, YAML_SCALAR_EVENT, value, TOKEN_LENGTH);

if (!strcmp(key, "board")) {
Expand Down
94 changes: 94 additions & 0 deletions local-gpio-v1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* 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;

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

if (!local_gpio->options->gpios[i].present)
continue;

local_gpio->gpios[i].chip =
gpiod_chip_open_lookup(local_gpio->options->gpios[i].chip);
if (!local_gpio->gpios[i].chip) {
err(1, "Unable to open gpiochip '%s'",
local_gpio->options->gpios[i].chip);
return -1;
}

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

if (local_gpio->options->gpios[i].active_low)
cfg.flags = GPIOD_LINE_REQUEST_FLAG_ACTIVE_LOW;

local_gpio->gpios[i].line = gpiod_chip_get_line(local_gpio->gpios[i].chip,
local_gpio->options->gpios[i].offset);

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

if (gpiod_line_request(local_gpio->gpios[i].line, &cfg, 0)) {
err(1, "Unable to request gpio %d offset %u",
i, local_gpio->options->gpios[i].offset);
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->gpios[gpio].line, on);
}
128 changes: 128 additions & 0 deletions local-gpio-v2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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.
*/
#define _GNU_SOURCE
#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;
int i;

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;
char *gpiochip_path;

if (!local_gpio->options->gpios[i].present)
continue;

if (asprintf(&gpiochip_path, "/dev/%s", local_gpio->options->gpios[i].chip) < 0) {
free(local_gpio);
return -1;
}

local_gpio->gpios[i].chip = gpiod_chip_open(gpiochip_path);
if (!local_gpio->gpios[i].chip) {
err(1, "Unable to open gpiochip '%s'", local_gpio->options->gpios[i].chip);
return -1;
}

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->options->gpios[i].active_low)
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->options->gpios[i].offset, 1,
line_settings) < 0) {
err(1, "Unable to set line config");
return -1;
}

local_gpio->gpios[i].line = gpiod_chip_request_lines(local_gpio->gpios[i].chip,
req_cfg, line_cfg);

if (!local_gpio->gpios[i].line) {
err(1, "Unable to request gpio %d offset %u",
i, local_gpio->options->gpios[i].offset);
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->gpios[gpio].line,
local_gpio->options->gpios[gpio].offset,
on ? GPIOD_LINE_VALUE_ACTIVE
: GPIOD_LINE_VALUE_INACTIVE);
}
Loading

0 comments on commit 87fbc38

Please sign in to comment.