| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * gpio-hammer - example swiss army knife to shake GPIO lines on a system |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2016 Linus Walleij |
|---|
| 5 | | - * |
|---|
| 6 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 7 | | - * under the terms of the GNU General Public License version 2 as published by |
|---|
| 8 | | - * the Free Software Foundation. |
|---|
| 9 | 6 | * |
|---|
| 10 | 7 | * Usage: |
|---|
| 11 | 8 | * gpio-hammer -n <device-name> -o <offset1> -o <offset2> |
|---|
| .. | .. |
|---|
| 25 | 22 | #include <linux/gpio.h> |
|---|
| 26 | 23 | #include "gpio-utils.h" |
|---|
| 27 | 24 | |
|---|
| 28 | | -int hammer_device(const char *device_name, unsigned int *lines, int nlines, |
|---|
| 25 | +int hammer_device(const char *device_name, unsigned int *lines, int num_lines, |
|---|
| 29 | 26 | unsigned int loops) |
|---|
| 30 | 27 | { |
|---|
| 31 | | - struct gpiohandle_data data; |
|---|
| 28 | + struct gpio_v2_line_values values; |
|---|
| 29 | + struct gpio_v2_line_config config; |
|---|
| 32 | 30 | char swirr[] = "-\\|/"; |
|---|
| 33 | 31 | int fd; |
|---|
| 34 | 32 | int ret; |
|---|
| 35 | 33 | int i, j; |
|---|
| 36 | 34 | unsigned int iteration = 0; |
|---|
| 37 | 35 | |
|---|
| 38 | | - memset(&data.values, 0, sizeof(data.values)); |
|---|
| 39 | | - ret = gpiotools_request_linehandle(device_name, lines, nlines, |
|---|
| 40 | | - GPIOHANDLE_REQUEST_OUTPUT, &data, |
|---|
| 41 | | - "gpio-hammer"); |
|---|
| 36 | + memset(&config, 0, sizeof(config)); |
|---|
| 37 | + config.flags = GPIO_V2_LINE_FLAG_OUTPUT; |
|---|
| 38 | + |
|---|
| 39 | + ret = gpiotools_request_line(device_name, lines, num_lines, |
|---|
| 40 | + &config, "gpio-hammer"); |
|---|
| 42 | 41 | if (ret < 0) |
|---|
| 43 | 42 | goto exit_error; |
|---|
| 44 | 43 | else |
|---|
| 45 | 44 | fd = ret; |
|---|
| 46 | 45 | |
|---|
| 47 | | - ret = gpiotools_get_values(fd, &data); |
|---|
| 46 | + values.mask = 0; |
|---|
| 47 | + values.bits = 0; |
|---|
| 48 | + for (i = 0; i < num_lines; i++) |
|---|
| 49 | + gpiotools_set_bit(&values.mask, i); |
|---|
| 50 | + |
|---|
| 51 | + ret = gpiotools_get_values(fd, &values); |
|---|
| 48 | 52 | if (ret < 0) |
|---|
| 49 | 53 | goto exit_close_error; |
|---|
| 50 | 54 | |
|---|
| 51 | 55 | fprintf(stdout, "Hammer lines ["); |
|---|
| 52 | | - for (i = 0; i < nlines; i++) { |
|---|
| 56 | + for (i = 0; i < num_lines; i++) { |
|---|
| 53 | 57 | fprintf(stdout, "%d", lines[i]); |
|---|
| 54 | | - if (i != (nlines - 1)) |
|---|
| 58 | + if (i != (num_lines - 1)) |
|---|
| 55 | 59 | fprintf(stdout, ", "); |
|---|
| 56 | 60 | } |
|---|
| 57 | 61 | fprintf(stdout, "] on %s, initial states: [", device_name); |
|---|
| 58 | | - for (i = 0; i < nlines; i++) { |
|---|
| 59 | | - fprintf(stdout, "%d", data.values[i]); |
|---|
| 60 | | - if (i != (nlines - 1)) |
|---|
| 62 | + for (i = 0; i < num_lines; i++) { |
|---|
| 63 | + fprintf(stdout, "%d", gpiotools_test_bit(values.bits, i)); |
|---|
| 64 | + if (i != (num_lines - 1)) |
|---|
| 61 | 65 | fprintf(stdout, ", "); |
|---|
| 62 | 66 | } |
|---|
| 63 | 67 | fprintf(stdout, "]\n"); |
|---|
| .. | .. |
|---|
| 66 | 70 | j = 0; |
|---|
| 67 | 71 | while (1) { |
|---|
| 68 | 72 | /* Invert all lines so we blink */ |
|---|
| 69 | | - for (i = 0; i < nlines; i++) |
|---|
| 70 | | - data.values[i] = !data.values[i]; |
|---|
| 73 | + for (i = 0; i < num_lines; i++) |
|---|
| 74 | + gpiotools_change_bit(&values.bits, i); |
|---|
| 71 | 75 | |
|---|
| 72 | | - ret = gpiotools_set_values(fd, &data); |
|---|
| 76 | + ret = gpiotools_set_values(fd, &values); |
|---|
| 73 | 77 | if (ret < 0) |
|---|
| 74 | 78 | goto exit_close_error; |
|---|
| 75 | 79 | |
|---|
| 76 | 80 | /* Re-read values to get status */ |
|---|
| 77 | | - ret = gpiotools_get_values(fd, &data); |
|---|
| 81 | + ret = gpiotools_get_values(fd, &values); |
|---|
| 78 | 82 | if (ret < 0) |
|---|
| 79 | 83 | goto exit_close_error; |
|---|
| 80 | 84 | |
|---|
| 81 | 85 | fprintf(stdout, "[%c] ", swirr[j]); |
|---|
| 82 | 86 | j++; |
|---|
| 83 | | - if (j == sizeof(swirr)-1) |
|---|
| 87 | + if (j == sizeof(swirr) - 1) |
|---|
| 84 | 88 | j = 0; |
|---|
| 85 | 89 | |
|---|
| 86 | 90 | fprintf(stdout, "["); |
|---|
| 87 | | - for (i = 0; i < nlines; i++) { |
|---|
| 88 | | - fprintf(stdout, "%d: %d", lines[i], data.values[i]); |
|---|
| 89 | | - if (i != (nlines - 1)) |
|---|
| 91 | + for (i = 0; i < num_lines; i++) { |
|---|
| 92 | + fprintf(stdout, "%d: %d", lines[i], |
|---|
| 93 | + gpiotools_test_bit(values.bits, i)); |
|---|
| 94 | + if (i != (num_lines - 1)) |
|---|
| 90 | 95 | fprintf(stdout, ", "); |
|---|
| 91 | 96 | } |
|---|
| 92 | 97 | fprintf(stdout, "]\r"); |
|---|
| .. | .. |
|---|
| 100 | 105 | ret = 0; |
|---|
| 101 | 106 | |
|---|
| 102 | 107 | exit_close_error: |
|---|
| 103 | | - gpiotools_release_linehandle(fd); |
|---|
| 108 | + gpiotools_release_line(fd); |
|---|
| 104 | 109 | exit_error: |
|---|
| 105 | 110 | return ret; |
|---|
| 106 | 111 | } |
|---|
| .. | .. |
|---|
| 124 | 129 | const char *device_name = NULL; |
|---|
| 125 | 130 | unsigned int lines[GPIOHANDLES_MAX]; |
|---|
| 126 | 131 | unsigned int loops = 0; |
|---|
| 127 | | - int nlines; |
|---|
| 132 | + int num_lines; |
|---|
| 128 | 133 | int c; |
|---|
| 129 | 134 | int i; |
|---|
| 130 | 135 | |
|---|
| .. | .. |
|---|
| 156 | 161 | |
|---|
| 157 | 162 | if (i >= GPIOHANDLES_MAX) { |
|---|
| 158 | 163 | fprintf(stderr, |
|---|
| 159 | | - "Only %d occurences of '-o' are allowed, %d were found\n", |
|---|
| 164 | + "Only %d occurrences of '-o' are allowed, %d were found\n", |
|---|
| 160 | 165 | GPIOHANDLES_MAX, i + 1); |
|---|
| 161 | 166 | return -1; |
|---|
| 162 | 167 | } |
|---|
| 163 | 168 | |
|---|
| 164 | | - nlines = i; |
|---|
| 169 | + num_lines = i; |
|---|
| 165 | 170 | |
|---|
| 166 | | - if (!device_name || !nlines) { |
|---|
| 171 | + if (!device_name || !num_lines) { |
|---|
| 167 | 172 | print_usage(); |
|---|
| 168 | 173 | return -1; |
|---|
| 169 | 174 | } |
|---|
| 170 | | - return hammer_device(device_name, lines, nlines, loops); |
|---|
| 175 | + return hammer_device(device_name, lines, num_lines, loops); |
|---|
| 171 | 176 | } |
|---|