GPIO ioctl

Started by FalloutST, April 09, 2021, 07:48:08 PM

Previous topic - Next topic

FalloutST

with example code
https://github.com/torvalds/linux/tree/master/tools/gpio

root@a64-olinuxino:/home/olimex/gpio-mon#  /home/olimex/gpio-mon/gpio-event-mon -n gpiochip0 -o 128 -b 10000
Failed to issue GPIO_GET_LINE_IOCTL (-22), Invalid argument

problem:
memset(&req, 0, sizeof(req));
for (i = 0; i < num_lines; i++)
    req.offsets[i] = lines[i];

req.config = *config;
strcpy(req.consumer, consumer);
req.num_lines = num_lines;

ret = ioctl(fd, GPIO_V2_GET_LINE_IOCTL, &req); <<== errno 22 EINVAL
if (ret == -1) {
    ret = -errno;
    fprintf(stderr, "Failed to issue %s (%d), %s\n",
        "GPIO_GET_LINE_IOCTL", ret, strerror(errno));
}

may be I did some thing wrong?

else no edge file on sysfs:

root@a64-olinuxino:/home/olimex/gpio-mon# echo 128 > /sys/class/gpio/export
root@a64-olinuxino:/home/olimex/gpio-mon# ls  /sys/class/gpio/gpio128/
active_low  device  direction  power  subsystem  uevent  value

FalloutST

First I found chip is wrong gpiochip1 is correct, but than:
root@a64-olinuxino:/home/olimex/gpio-mon# ./gpio-mon -n gpiochip1 -o 128 -r -f -b 10000
Failed to issue GPIO_GET_LINE_IOCTL (-16), Device or resource busy
root@a64-olinuxino:/home/olimex/gpio-mon# echo 128 > /sys/class/gpio/unexport
root@a64-olinuxino:/home/olimex/gpio-mon# ./gpio-mon -n gpiochip1 -o 128 -r -f -b 10000
Failed to issue GPIO_GET_LINE_IOCTL (-6), No such device or address

FalloutST

#2
"No such device or address." The system tried to use the device represented by a file you specified, and it couldn't find the device. This can mean that the device file was installed incorrectly, or that the physical device is missing or not correctly attached to the computer.

Does that mean sun50i-a64-pinctrl /soc/pinctrl@lc20800 incorrectly loaded or...

JohnS

I'm guessing someone will want to know exactly which Linux image you're using and may also want to know what lsgpio in those tools shows.

John

FalloutST

#4
root@a64-olinuxino:/home/olimex/gpio-mon# uname -a
Linux a64-olinuxino 5.10.23-olimex #115137 SMP Thu Mar 18 11:52:54 UTC 2021 aarch64 GNU/Linux
same with 5.8.18
GPIO chip: gpiochip2, "axp20x-gpio", 2 GPIO lines
        line  0: unnamed unused [output]
        line  1: unnamed unused [output]
GPIO chip: gpiochip1, "1c20800.pinctrl", 256 GPIO lines
        line  0: unnamed unused [input]
        line  1: unnamed unused [input]
        line  2: unnamed unused [input]
        line  3: unnamed unused [input]
        line  4: unnamed unused [input]
        line  5: unnamed unused [input]
        line  6: unnamed unused [input]
        line  7: unnamed unused [input]
        line  8: unnamed unused [input]
        line  9: unnamed unused [input]
        line 10: unnamed unused [input]
        line 11: unnamed unused [input]
        line 12: unnamed unused [input]
        line 13: unnamed unused [input]
        line 14: unnamed unused [input]
        line 15: unnamed unused [input]
        line 16: unnamed unused [input]
        line 17: unnamed unused [input]
        line 18: unnamed unused [input]
        line 19: unnamed unused [input]
        line 20: unnamed unused [input]
        line 21: unnamed unused [input]
        line 22: unnamed unused [input]
        line 23: unnamed unused [input]
        line 24: unnamed unused [input]
        line 25: unnamed unused [input]
        line 26: unnamed unused [input]
        line 27: unnamed unused [input]
        line 28: unnamed unused [input]
        line 29: unnamed unused [input]
        line 30: unnamed unused [input]
        line 31: unnamed unused [input]
        line 32: unnamed unused [used, input]
        line 33: unnamed unused [used, input]
        line 34: unnamed unused [input]
        line 35: unnamed unused [input]
        line 36: unnamed unused [input]
        line 37: unnamed unused [input]
        line 38: unnamed unused [input]
        line 39: unnamed unused [input]
        line 40: unnamed unused [used, input]
        line 41: unnamed unused [used, input]
        line 42: unnamed unused [input]
        line 43: unnamed unused [input]
        line 44: unnamed unused [input]
        line 45: unnamed unused [input]
        line 46: unnamed unused [input]
        line 47: unnamed unused [input]
        line 48: unnamed unused [input]
        line 49: unnamed unused [input]
        line 50: unnamed unused [input]
        line 51: unnamed unused [input]
        line 52: unnamed unused [input]
        line 53: unnamed unused [input]
        line 54: unnamed unused [input]
        line 55: unnamed unused [input]
        line 56: unnamed unused [input]
        line 57: unnamed unused [input]
        line 58: unnamed unused [input]
        line 59: unnamed unused [input]
        line 60: unnamed unused [input]
        line 61: unnamed unused [input]
        line 62: unnamed unused [input]
        line 63: unnamed unused [input]
        line 64: unnamed unused [used, input]
        line 65: unnamed unused [used, input]
        line 66: unnamed unused [used, input]
        line 67: unnamed unused [used, input]
        line 68: unnamed unused [input]
        line 69: unnamed unused [input]
        line 70: unnamed unused [input]
        line 71: unnamed unused [input]
        line 72: unnamed unused [input]
        line 73: unnamed unused [input]
        line 74: unnamed unused [input]
        line 75: unnamed unused [input]
        line 76: unnamed unused [input]
        line 77: unnamed unused [input]
        line 78: unnamed unused [input]
        line 79: unnamed unused [input]
        line 80: unnamed unused [input]
        line 81: unnamed unused [input]
        line 82: unnamed unused [input]
        line 83: unnamed unused [input]
        line 84: unnamed unused [input]
        line 85: unnamed unused [input]
        line 86: unnamed unused [input]
        line 87: unnamed unused [input]
        line 88: unnamed unused [input]
        line 89: unnamed unused [input]
        line 90: unnamed unused [input]
        line 91: unnamed unused [input]
        line 92: unnamed unused [input]
        line 93: unnamed unused [input]
        line 94: unnamed unused [input]
        line 95: unnamed unused [input]
        line 96: unnamed unused [input]
        line 97: unnamed unused [input]
        line 98: unnamed unused [input]
        line 99: unnamed unused [input]
        line 100: unnamed unused [input]
        line 101: unnamed unused [input]
        line 102: unnamed unused [input]
        line 103: unnamed unused [input]
        line 104: unnamed unused [used, input]
        line 105: unnamed unused [used, input]
        line 106: unnamed unused [used, input]
        line 107: unnamed unused [used, input]
        line 108: unnamed unused [used, input]
        line 109: unnamed unused [used, input]
        line 110: unnamed unused [input]
        line 111: unnamed unused [used, input]
        line 112: unnamed unused [used, input]
        line 113: unnamed unused [used, input]
        line 114: unnamed unused [used, input]
        line 115: unnamed unused [used, input]
        line 116: unnamed unused [used, input]
        line 117: unnamed unused [used, input]
        line 118: unnamed unused [used, input]
        line 119: unnamed unused [used, input]
        line 120: unnamed unused [input]
        line 121: unnamed unused [input]
        line 122: unnamed unused [input]
        line 123: unnamed unused [input]
        line 124: unnamed unused [input]
        line 125: unnamed unused [input]
        line 126: unnamed unused [input]
        line 127: unnamed unused [input]
        line 128: unnamed unused [input]
        line 129: unnamed unused [input]
        line 130: unnamed unused [input]
        line 131: unnamed unused [input]
        line 132: unnamed unused [input]
        line 133: unnamed unused [input]
        line 134: unnamed unused [input]
        line 135: unnamed unused [input]
        line 136: unnamed unused [input]
        line 137: unnamed unused [input]
        line 138: unnamed unused [input]
        line 139: unnamed unused [input]
        line 140: unnamed unused [input]
        line 141: unnamed unused [input]
        line 142: unnamed unused [input]
        line 143: unnamed unused [input]
        line 144: unnamed unused [input]
        line 145: unnamed unused [input]
        line 146: unnamed unused [input]
        line 147: unnamed unused [input]
        line 148: unnamed unused [input]
        line 149: unnamed unused [input]
        line 150: unnamed unused [input]
        line 151: unnamed unused [input]
        line 152: unnamed unused [input]
        line 153: unnamed unused [input]
        line 154: unnamed unused [input]
        line 155: unnamed unused [input]
        line 156: unnamed unused [input]
        line 157: unnamed unused [input]
        line 158: unnamed unused [input]
        line 159: unnamed unused [input]
        line 160: unnamed unused [used, input]
        line 161: unnamed unused [used, input]
        line 162: unnamed unused [used, input]
        line 163: unnamed unused [used, input]
        line 164: unnamed unused [used, input]
        line 165: unnamed unused [used, input]
        line 166: unnamed "cd" [used, input, active-low]
        line 167: unnamed unused [input]
        line 168: unnamed unused [input]
        line 169: unnamed unused [input]
        line 170: unnamed unused [input]
        line 171: unnamed unused [input]
        line 172: unnamed unused [input]
        line 173: unnamed unused [input]
        line 174: unnamed unused [input]
        line 175: unnamed unused [input]
        line 176: unnamed unused [input]
        line 177: unnamed unused [input]
        line 178: unnamed unused [input]
        line 179: unnamed unused [input]
        line 180: unnamed unused [input]
        line 181: unnamed unused [input]
        line 182: unnamed unused [input]
        line 183: unnamed unused [input]
        line 184: unnamed unused [input]
        line 185: unnamed unused [input]
        line 186: unnamed unused [input]
        line 187: unnamed unused [input]
        line 188: unnamed unused [input]
        line 189: unnamed unused [input]
        line 190: unnamed unused [input]
        line 191: unnamed unused [input]
        line 192: unnamed unused [input]
        line 193: unnamed unused [input]
        line 194: unnamed unused [input]
        line 195: unnamed unused [input]
        line 196: unnamed unused [input]
        line 197: unnamed unused [input]
        line 198: unnamed unused [input]
        line 199: unnamed unused [input]
        line 200: unnamed unused [input]
        line 201: unnamed "usb1-vbus" [used, output]
        line 202: unnamed unused [input]
        line 203: unnamed unused [input]
        line 204: unnamed unused [input]
        line 205: unnamed unused [input]
        line 206: unnamed unused [input]
        line 207: unnamed unused [input]
        line 208: unnamed unused [input]
        line 209: unnamed unused [input]
        line 210: unnamed unused [input]
        line 211: unnamed unused [input]
        line 212: unnamed unused [input]
        line 213: unnamed unused [input]
        line 214: unnamed unused [input]
        line 215: unnamed unused [input]
        line 216: unnamed unused [input]
        line 217: unnamed unused [input]
        line 218: unnamed unused [input]
        line 219: unnamed unused [input]
        line 220: unnamed unused [input]
        line 221: unnamed unused [input]
        line 222: unnamed unused [input]
        line 223: unnamed unused [input]
        line 224: unnamed unused [used, input]
        line 225: unnamed unused [used, input]
        line 226: unnamed unused [input]
        line 227: unnamed unused [input]
        line 228: unnamed unused [used, input]
        line 229: unnamed unused [used, input]
        line 230: unnamed "usb0_vbus_det" [used, input]
        line 231: unnamed unused [input]
        line 232: unnamed unused [input]
        line 233: unnamed "usb0_id_det" [used, input]
        line 234: unnamed unused [input]
        line 235: unnamed unused [input]
        line 236: unnamed unused [input]
        line 237: unnamed unused [input]
        line 238: unnamed unused [input]
        line 239: unnamed unused [input]
        line 240: unnamed unused [input]
        line 241: unnamed unused [input]
        line 242: unnamed unused [input]
        line 243: unnamed unused [input]
        line 244: unnamed unused [input]
        line 245: unnamed unused [input]
        line 246: unnamed unused [input]
        line 247: unnamed unused [input]
        line 248: unnamed unused [input]
        line 249: unnamed unused [input]
        line 250: unnamed unused [input]
        line 251: unnamed unused [input]
        line 252: unnamed unused [input]
        line 253: unnamed unused [input]
        line 254: unnamed unused [input]
        line 255: unnamed unused [input]
GPIO chip: gpiochip0, "1f02c00.pinctrl", 32 GPIO lines
        line  0: unnamed unused [used, input]
        line  1: unnamed unused [used, input]
        line  2: unnamed unused [input]
        line  3: unnamed unused [input]
        line  4: unnamed unused [input]
        line  5: unnamed unused [input]
        line  6: unnamed unused [input]
        line  7: unnamed unused [input]
        line  8: unnamed unused [input]
        line  9: unnamed unused [input]
        line 10: unnamed unused [input]
        line 11: unnamed unused [input]
        line 12: unnamed unused [input]
        line 13: unnamed unused [input]
        line 14: unnamed unused [input]
        line 15: unnamed unused [input]
        line 16: unnamed unused [input]
        line 17: unnamed unused [input]
        line 18: unnamed unused [input]
        line 19: unnamed unused [input]
        line 20: unnamed unused [input]
        line 21: unnamed unused [input]
        line 22: unnamed unused [input]
        line 23: unnamed unused [input]
        line 24: unnamed unused [input]
        line 25: unnamed unused [input]
        line 26: unnamed unused [input]
        line 27: unnamed unused [input]
        line 28: unnamed unused [input]
        line 29: unnamed unused [input]
        line 30: unnamed unused [input]
        line 31: unnamed unused [input]

JohnS

I'm wondering why you'd use
-o 128

John

FalloutST

#6
Quote from: JohnS on April 10, 2021, 04:43:00 PMI'm wondering why you'd use
-o 128

John

root@a64-olinuxino:/home/olimex/gpio-mon# ./gpio-event-mon
Usage: gpio-event-mon [options]...
Listen to events on GPIO lines, 0->1 1->0
  -n <name>  Listen on GPIOs on a named device (must be stated)
  -o <n>     Offset of line to monitor (may be repeated)
  -d         Set line as open drain
  -s         Set line as open source
  -r         Listen for rising edges
  -f         Listen for falling edges
  -w         Report the wall-clock time for events
  -b <n>     Debounce the line with period n microseconds
 [-c <n>]    Do <n> loops (optional, infinite loop if not stated)
  -?         This helptext

Example:
gpio-event-mon -n gpiochip0 -o 4 -r -f -b 10000

so some reserch:
~kernel-src~/kernel/drivers/pinctrl/sunxi/pinctrl-sun50i-a64.c
...
 SUNXI_PIN(SUNXI_PINCTRL_PIN(E, 0),
   SUNXI_FUNCTION(0x0, "gpio_in"),
   SUNXI_FUNCTION(0x1, "gpio_out"),
   SUNXI_FUNCTION(0x2, "csi"), /* PCK */
   SUNXI_FUNCTION(0x4, "ts")), /* CLK */
...
after I found that out
I look four IRQ pin like on port B but that port used as uart on 0-3 pins
any way i have available pin 4
...
 SUNXI_PIN(SUNXI_PINCTRL_PIN(B, 4),
   SUNXI_FUNCTION(0x0, "gpio_in"),
   SUNXI_FUNCTION(0x1, "gpio_out"),
   SUNXI_FUNCTION(0x2, "aif2"), /* SYNC */
   SUNXI_FUNCTION(0x3, "i2s0"), /* SYNC */
   SUNXI_FUNCTION(0x5, "sim"), /* CLK */
   SUNXI_FUNCTION_IRQ_BANK(0x6, 0, 4)), /* EINT4 */
...
Quote from: JohnS on April 10, 2021, 04:43:00 PMI'm wondering why you'd use
-o 128

John
ASCII 'A' =65
ASCII 'B' =66

(66-65) * 32+ 4 = 36

so my UNNAMED in configs pin number is 36
root@a64-olinuxino:/home/olimex/gpio-mon# echo 36 > /sys/class/gpio/export
root@a64-olinuxino:/home/olimex/gpio-mon# ls /sys/class/gpio
export  gpio36  gpiochip0  gpiochip352  gpiochip510  unexport
root@a64-olinuxino:/home/olimex/gpio-mon# ls /sys/class/gpio/gpio36
active_low  device  direction  edge  power  subsystem  uevent  value
edge  file comes up
root@a64-olinuxino:/home/olimex/gpio-mon# ./gpio-event-mon -n gpiochip1 -o 36
No flags specified, listening on both rising and falling edges
Failed to issue GPIO_GET_LINE_IOCTL (-16), Device or resource busy
root@a64-olinuxino:/home/olimex/gpio-mon# echo 36 > /sys/class/gpio/unexport
root@a64-olinuxino:/home/olimex/gpio-mon# ./gpio-event-mon -n gpiochip1 -o 36
No flags specified, listening on both rising and falling edges
Monitoring line 36 on gpiochip1
Initial line value: 0
GPIO EVENT at 66075293284877 on line 36 (1|1) rising edge
GPIO EVENT at 66077323552113 on line 36 (2|2) falling edge
^C
root@a64-olinuxino:/home/olimex/gpio-mon#
SOLVED but no IRQ on port E is very bad option i've found on industrial machine

LubOlimex

#7
This is correct IRQs are not defined in the device tree by default (edit: because they can't be interrupts). Not all pins can be used as IRQs, only free pins with EINT name in the schematic and in the A64 datasheet here:

http://dl.linux-sunxi.org/A64/A64_Datasheet_V1.1.pdf
Technical support and documentation manager at Olimex

FalloutST

#8
can I rewrite FUNCTION_IRQ_BANK without rebuilding kernel?
or there hw limitation?
--------------------------------------
other question have you try real time patches?

LubOlimex

Which GPIO do you want to use? Does it have EINT?
Technical support and documentation manager at Olimex

LubOlimex

Of course there is hardware limitation, some pins can't be interrupts. Look at the datasheet of the chip.
Technical support and documentation manager at Olimex

FalloutST

Quote from: LubOlimex on April 12, 2021, 03:12:38 PMOf course there is hardware limitation, some pins can't be interrupts. Look at the datasheet of the chip.
10  PB0 UART2_TX
12  PB1 UART2_RX
14  PB2 UART2_RTS
16  PB3 UART2_CTS
18  PB4 GPIO 1*32+4 #36 - have IRQ & not UART2
SUNXI_PIN(SUNXI_PINCTRL_PIN(B, 4),
   SUNXI_FUNCTION(0x0, "gpio_in"),
   SUNXI_FUNCTION(0x1, "gpio_out"),
   SUNXI_FUNCTION(0x2, "aif2"), /* SYNC */
   SUNXI_FUNCTION(0x3, "i2s0"), /* SYNC */
   SUNXI_FUNCTION(0x5, "sim"), /* CLK */
   SUNXI_FUNCTION_IRQ_BANK(0x6, 0, 4)), /* EINT4 */
what about real time kernel?

FalloutST

Port E no IRQ from hw limitation?

FalloutST

#13
Quote from: FalloutST on April 12, 2021, 05:21:48 PMPort E no IRQ from hw limitation?
PE1 I/O DIS Z CSI_MCLK - TS_ERR
PE2 I/O DIS Z CSI_HSYNC - TS_SYNC
PE3 I/O DIS Z CSI_VSYNC - TS_DVLD
PE4 I/O DIS Z CSI_D0 - TS_D0
PE5 I/O DIS Z CSI_D1 - TS_D1
PE6 I/O DIS Z CSI_D2 - TS_D2
PE7 I/O DIS Z CSI_D3 - TS_D3
PE8 I/O DIS Z CSI_D4 - TS_D4
PE9 I/O DIS Z CSI_D5 - TS_D5
Can i rebuild pinctrl@lc20800 driver without rebuilding kernel?

A64 manual page 213
GPU-GP 129 0x0204 GPU-GP interrupt
GPU-GPMMU 130 0x0208 GPU-GPMMU interrupt
GPU-PP0 131 0x020C GPU-PP0 interrupt
GPU-PP0MMU 132 0x0210 GPU-PPMMU0 interrupt
GPU-PMU 133 0x0214 GPU-PMU interrupt
GPU-PP1 134 0x0218 GPU-PP1 interrupt
GPU-PPMMU1 135 0x021C GPU-PPMMU1 interrupt
136 0x0220
137 0x0224
138 0x0228
139 0x022C
CTI0 140 0x0230 CTI0 interrupt
CTI1 141 0x0234 CTI1 interrupt
CTI2 142 0x0238 CTI2 interrupt
CTI3 143 0x023C CTI3 interrupt
COMMTX0 144 0x0240 COMMTX0 interrupt

LubOlimex

Yes - Port C, Port D, Port E, Port F - don't have interrupt function.

Only Port B, Port G, Port H have interrupt function:



Notice that some of the pins can be used for other peripherals with another function, before trying to use them for interrupts, make sure to check the schematic for possible multiplexing.
Technical support and documentation manager at Olimex