|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Linaro Ltd. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * 3. Neither the name of the copyright holder nor the names of its contributors |
| 16 | + * may be used to endorse or promote products derived from this software without |
| 17 | + * specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | +#define _GNU_SOURCE |
| 32 | +#include <ctype.h> |
| 33 | +#include <err.h> |
| 34 | +#include <errno.h> |
| 35 | +#include <fcntl.h> |
| 36 | +#include <stdio.h> |
| 37 | +#include <string.h> |
| 38 | +#include <stdlib.h> |
| 39 | +#include <unistd.h> |
| 40 | +#include <stdbool.h> |
| 41 | + |
| 42 | +/* local-gpio implementation for libgpiod major version 2 */ |
| 43 | + |
| 44 | +#include "local-gpio.h" |
| 45 | + |
| 46 | +#include <gpiod.h> |
| 47 | + |
| 48 | +int local_gpio_init(struct local_gpio *local_gpio) |
| 49 | +{ |
| 50 | + struct gpiod_request_config *req_cfg; |
| 51 | + int i; |
| 52 | + |
| 53 | + req_cfg = gpiod_request_config_new(); |
| 54 | + if (!req_cfg) { |
| 55 | + err(1, "Unable to allocate request config"); |
| 56 | + return -1; |
| 57 | + } |
| 58 | + gpiod_request_config_set_consumer(req_cfg, "cdba"); |
| 59 | + |
| 60 | + for (i = 0; i < GPIO_COUNT; ++i) { |
| 61 | + struct gpiod_line_settings *line_settings; |
| 62 | + struct gpiod_line_config *line_cfg; |
| 63 | + char *gpiochip_path; |
| 64 | + |
| 65 | + if (!local_gpio->options->gpios[i].present) |
| 66 | + continue; |
| 67 | + |
| 68 | + if (asprintf(&gpiochip_path, "/dev/%s", local_gpio->options->gpios[i].chip) < 0) { |
| 69 | + free(local_gpio); |
| 70 | + return -1; |
| 71 | + } |
| 72 | + |
| 73 | + local_gpio->gpios[i].chip = gpiod_chip_open(gpiochip_path); |
| 74 | + if (!local_gpio->gpios[i].chip) { |
| 75 | + err(1, "Unable to open gpiochip '%s'", local_gpio->options->gpios[i].chip); |
| 76 | + return -1; |
| 77 | + } |
| 78 | + |
| 79 | + line_settings = gpiod_line_settings_new(); |
| 80 | + if (!line_settings) { |
| 81 | + err(1, "Unable to allocate gpio line settings"); |
| 82 | + return -1; |
| 83 | + } |
| 84 | + if (gpiod_line_settings_set_direction(line_settings, |
| 85 | + GPIOD_LINE_DIRECTION_OUTPUT) < 0) { |
| 86 | + err(1, "Unable to set line direction"); |
| 87 | + return -1; |
| 88 | + } |
| 89 | + if (local_gpio->options->gpios[i].active_low) |
| 90 | + gpiod_line_settings_set_active_low(line_settings, true); |
| 91 | + if (gpiod_line_settings_set_output_value(line_settings, |
| 92 | + GPIOD_LINE_VALUE_INACTIVE) < 0) { |
| 93 | + err(1, "Unable to set line output value"); |
| 94 | + return -1; |
| 95 | + } |
| 96 | + |
| 97 | + line_cfg = gpiod_line_config_new(); |
| 98 | + if (!line_cfg) { |
| 99 | + err(1, "Unable to allocate gpio line settings"); |
| 100 | + return -1; |
| 101 | + } |
| 102 | + if (gpiod_line_config_add_line_settings(line_cfg, |
| 103 | + &local_gpio->options->gpios[i].offset, 1, |
| 104 | + line_settings) < 0) { |
| 105 | + err(1, "Unable to set line config"); |
| 106 | + return -1; |
| 107 | + } |
| 108 | + |
| 109 | + local_gpio->gpios[i].line = gpiod_chip_request_lines(local_gpio->gpios[i].chip, |
| 110 | + req_cfg, line_cfg); |
| 111 | + |
| 112 | + if (!local_gpio->gpios[i].line) { |
| 113 | + err(1, "Unable to request gpio %d offset %u", |
| 114 | + i, local_gpio->options->gpios[i].offset); |
| 115 | + return -1; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return 0; |
| 120 | +} |
| 121 | + |
| 122 | +int local_gpio_set_value(struct local_gpio *local_gpio, unsigned int gpio, bool on) |
| 123 | +{ |
| 124 | + return gpiod_line_request_set_value(local_gpio->gpios[gpio].line, |
| 125 | + local_gpio->options->gpios[gpio].offset, |
| 126 | + on ? GPIOD_LINE_VALUE_ACTIVE |
| 127 | + : GPIOD_LINE_VALUE_INACTIVE); |
| 128 | +} |
0 commit comments