# Accessing GPIO on PC Engines APU.3B4



## Sergey.m (Oct 13, 2017)

I'm trying to use GPIO functionality provided by nct5104d chip on the APU board. 

I have installed FreeBSD 11 with nctgpio and gpiobus modules loaded. However, when I'm trying to run gpioctl, I'm getting an error message, and no GPIO device exists in /dev:


```
root@apu-freebsd:/usr/home/conveyor # kldstat
Id Refs Address            Size     Name
 1    6 0xffffffff80200000 1f67a88  kernel
 2    1 0xffffffff82221000 20f2     nctgpio.ko
 3    1 0xffffffff82224000 3a23     gpiobus.ko
root@apu-freebsd:/usr/home/conveyor # gpioctl -lv
gpio_open: No such file or directory
root@apu-freebsd:/usr/home/conveyor # ls /dev/gp*
ls: No match.
```

I have BIOS v4.0.11 (20170724) and both UART C and D disabled (though I've tried to toggle them back in forth without any difference).

Did anyone run into a similar problem before?

Thanks!


----------



## Phishfry (Oct 13, 2017)

I have used nctgpio with APU1 and APU2. I will test with APU3 and let you know.

There is an issue where the gpio is slightly different for the APU3. They use a gpio pin for switching SIM sockets electrically.
With the APU3 having 2 SIM sockets. So you can switch cellular networks via gpio.


----------



## Sergey.m (Oct 13, 2017)

I actually just have figured it out. The release of the board that I have has a slightly different Chip ID for nct5104d  - it is 0xc453, while nctgpio driver expects 0xc452.

I don't know how to patch a module in FreeBSD, but I was able to access GPIO in Debian after making a similar patch to the driver.


----------



## Phishfry (Oct 13, 2017)

/usr/src/sys/dev/nctgpio/nctgpio.c

Here is what you need to mod on FreeBSD source- then recompile:

```
struct nuvoton_vendor_device_id {
   uint16_t       chip_id;
   const char *       descr;
} nct_devs[] = {
   {
       .chip_id   = 0x1061,
       .descr       = "Nuvoton NCT5104D",
   },
   {
       .chip_id   = 0xc452,
       .descr       = "Nuvoton NCT5104D (PC-Engines APU)",
   },
};
```


----------



## Phishfry (Oct 13, 2017)

That is strange as my APU3B2 works:


```
root@APU3:~ # dmesg | grep 'gpio'
gpio0: <Nuvoton NCT5104D (PC-Engines APU)> at port 0x2e-0x2f on isa0
gpiobus0: <GPIO bus> on gpio0
gpioc0: <GPIO controller> on gpio0
root@APU3:~ # gpioctl -lv
pin 00:   1   GPIO00<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 01:   1   GPIO01<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 02:   1   GPIO02<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 03:   1   GPIO03<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 04:   1   GPIO04<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 05:   1   GPIO05<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 06:   1   GPIO06<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 07:   1   GPIO07<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 08:   0   GPIO08<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 09:   0   GPIO09<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 10:   0   GPIO10<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 11:   0   GPIO11<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 12:   0   GPIO12<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 13:   0   GPIO13<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 14:   0   GPIO14<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
pin 15:   0   GPIO15<IN,OD>, caps:<IN,OUT,OD,PP,II,IO>
```


----------



## Phishfry (Oct 18, 2017)

Just a follow-up. My newest APU3 with 4G RAM fails the same with GPIO a different chip id.
My APU3 with 2G works with the old chip id. Maybe an older revision of the board. I got it 4 months ago.


----------



## Mike Tancsa (Dec 27, 2017)

https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=224512
I submitted a patch in the PR for the new board rev. Hopefully someone will merge it


----------



## Phishfry (Dec 27, 2017)

I will check it out and leave feedback on the PR. Have you bumped anybody yet? I can ask Gonzo to commit it.
He is pretty active in the embedded world.


----------



## Mike Tancsa (Dec 28, 2017)

I havent yet pestered anyone.  Just to add to the search engines, once you get the driver working, if your ultimate goal is to get the sim swap working, all the code that is needed is here 
https://github.com/pcengines/apu_gpio_lib


----------



## Phishfry (Jan 7, 2018)

https://svnweb.freebsd.org/base?view=revision&revision=327502
Gonzo is really a shinning star of FreeBSD. He committed this only hours after I asked.
It is nice to have a working APU GPIO driver across all models.


----------



## Phishfry (Feb 26, 2018)

Gonzo pushed the merge for the APU3 patch so it is in 11-Stable now and should end up in FreeBSD 11.2
https://svnweb.freebsd.org/base?view=revision&revision=329603


----------

