how to access GPIO on imx28

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how to access GPIO on imx28

Jump to solution
7,534 Views
yongkimin
Contributor III

Hi I'm developing with imx28 (i.MX28) platform. BSP version is L2.6.35_10.12.01.

I'd like to access gpio port on user space.

So I did configuration of kernel. And I checked config file that show the flag "CONFIG_GPIO_SYSFS=y".

But I can't see the sysfs on /sys/class/gpio. This is my NFS mounted directory of Ubuntu host.

root@freescale /sys/class$ ls

backlight     input         net           scsi_device   vc

bdi           leds          power_supply  scsi_disk     video4linux

block         mdio_bus      raw           scsi_host     vtconsole

bsg           mem           rc            sound

firmware      misc          regulator     spi_master

graphics      mmc_host      rfkill        tty

i2c-adapter   mtd           rtc           ubi

After the configuration I rebuild the image. The image name rebuilt is rootfs.ext2.gz.

This is copy to NFS directory (/tools/rootfs).

Can anybody tell me how to access /sys/class/gpio sysfs.

Thanks.

Labels (2)
0 Kudos
1 Solution
2,931 Views
yongkimin
Contributor III

I solved this.

At first I say thanks to Yuri Muhin and Emanuele Coli answer (https://community.freescale.com/message/312813#312813).

apply the configuration CONFIG_GPIO_SYSFS=y

after build the image(uImage) and update tftp directory.

and then you will see the /sys/class/gpio sysfs.


change the source code /arch/arm/mach-mx28/mx28evk_pins.c In my case   

{

  .name = "SSP0_DATA4",

  .id = PINID_SSP0_DATA4,

  .fun = PIN_GPIO,

  .strength = PAD_4MA,

  .voltage = PAD_3_3V,

  .pullup = 1,

  .drive = 1,

  .pull = 1,

  .data = 1,

  .output = 1,

},

and after modify the source code call the gpio_export function like as.

status = gpio_request(MXS_PIN_TO_GPIO(PINID_SSP0_DATA4), "GPS_RESET");

/* already requested ? */

if (status == -EBUSY) {

    gpio_export(MXS_PIN_TO_GPIO(PINID_SSP0_DATA4), false);

}

In my case without this I can't see /sys/class/gpio/gpioXX.

root@freescale /sys/class/gpio$ ls

export       gpiochip0    gpiochip32 gpiochip96

gpio68       gpiochip128  gpiochip64 unexport

Now I can change the value on GPIO using this command.

root@freescale /module_test$ echo 0 > /sys/class/gpio/gpio68/value

root@freescale /module_test$ echo 1 > /sys/class/gpio/gpio68/value

I did verify that with Oscilloscpe. I hope this report make a help to others.

Thanks.

View solution in original post

0 Kudos
3 Replies
2,932 Views
yongkimin
Contributor III

I solved this.

At first I say thanks to Yuri Muhin and Emanuele Coli answer (https://community.freescale.com/message/312813#312813).

apply the configuration CONFIG_GPIO_SYSFS=y

after build the image(uImage) and update tftp directory.

and then you will see the /sys/class/gpio sysfs.


change the source code /arch/arm/mach-mx28/mx28evk_pins.c In my case   

{

  .name = "SSP0_DATA4",

  .id = PINID_SSP0_DATA4,

  .fun = PIN_GPIO,

  .strength = PAD_4MA,

  .voltage = PAD_3_3V,

  .pullup = 1,

  .drive = 1,

  .pull = 1,

  .data = 1,

  .output = 1,

},

and after modify the source code call the gpio_export function like as.

status = gpio_request(MXS_PIN_TO_GPIO(PINID_SSP0_DATA4), "GPS_RESET");

/* already requested ? */

if (status == -EBUSY) {

    gpio_export(MXS_PIN_TO_GPIO(PINID_SSP0_DATA4), false);

}

In my case without this I can't see /sys/class/gpio/gpioXX.

root@freescale /sys/class/gpio$ ls

export       gpiochip0    gpiochip32 gpiochip96

gpio68       gpiochip128  gpiochip64 unexport

Now I can change the value on GPIO using this command.

root@freescale /module_test$ echo 0 > /sys/class/gpio/gpio68/value

root@freescale /module_test$ echo 1 > /sys/class/gpio/gpio68/value

I did verify that with Oscilloscpe. I hope this report make a help to others.

Thanks.

0 Kudos
2,931 Views
Yuri
NXP Employee
NXP Employee

General consideration are as following.

Although you can develop your own driver to control GPIOs inside kernel space, there is a much simpler way for accessing GPIOs from user space. When timing requirements are not an issue, you are able to use GPIO-SYSFS. 
SYSFS is a virtual file system that exports some kernel internal framework functionalities to user space and GPIO is one of the frameworks that can have functionalities exported through SYSFS.

The GPIO-SYSFS feature is available in all mainline kernels from 2.6.27 onwards.

Configuring Kernel to export GPIO through SYSFS

To enable GPIO in SYSFS, select the following kernel option:

Device Drivers --->

      --- GPIO Support

            [*] /sys/class/gpio/... (sysfs interface)

If you are using i.MX233 or i.MX28, after recompiling the kernel, do not forget to generate boot streams again,

because this is not automatic even in ltib. Be sure that the pins you will try to use are really accessible as GPIO pins

and were not requested by the kernel (gpio_request). If pin was gpio_request'ed, you will need to gpio_export the same

pin inside the kernel in order to have it accessible through SYSFS. If pin is not set as GPIO by default, you will need to set

IO MUX in the proper file inside <kernel>/arch/arm/mach-XXX. For i.MX28 EVK this is


<ltib_dir>/rpm/BUILD/linux/arch/arm/mach-mx28/mx28evk_pins.c 

Accessing GPIO in user space

After enabling GPIO-SYSFS feature, you can boot your device with the new kernel to make some tests.

First you need to export the GPIO you want to test to the user space:

echo XX > /sys/class/gpio/export

XX shall be determined by the following algorithm:

GPIOA_[B] is the GPIO you want to export, where "A" is the GPIO bank and "B" is the offset of the pin in the bank.

if the first available GPIO bank is 0 // (iMX.28, for example)

    XX = A*32 + B;

else // first GPIO bank is 1

    XX = (A-1)*32 + B;

After exporting a GPIO pin, you shall be able to see the GPIO interface exported to:

/sys/class/gpio/gpioXX

Through this interface, you are now able to do things like:

# Reading the pin value

cat /sys/class/gpio/gpioXX/value

# Changing pin direction

echo in > /sys/class/gpio/gpioXX/direction

echo out > /sys/class/gpio/gpioXX/direction

# Toggling GPIO output level

echo 0 > /sys/class/gpio/gpioXX/value

echo 1 > /sys/class/gpio/gpioXX/value

It is important to note that through the GPIO virtual filesystem it is only possible to deal with

one GPIO pin at a time (per command).

2,931 Views
yongkimin
Contributor III

Thank you Yuri.

I did Kernel Configuration for GPIO. I checked in configuration files in ./config directory.

ykmin@ubuntu:~/L2.6.35_10.12.01_ER_source/LTIB/ltib$ grep -rwn "CONFIG_GPIO_SYSFS" ./config

./config/platform/imx/imx28evk_updater_defconfig.dev:853:# CONFIG_GPIO_SYSFS is not set

./config/platform/imx/imx28evk_defconfig.dev:1077:CONFIG_GPIO_SYSFS=y

I did rebuild the image ./ltib -f command. (Is this right?)

I don't modify yet in <ltib_dir>/rpm/BUILD/linux/arch/arm/mach-mx28/mx28evk_pins.c

Rebuilded image (rootfs.ext2.gz) copy to NFS directory (/tools/rootfs).

I connect my board to NFS directory and I check if /sys/class/gpio sysfs. But not founded.

Can you tell me what mean you tell me.

If pin was gpio_request'ed, you will need to gpio_export the same

pin inside the kernel in order to have it accessible through SYSFS.

Thank you.

0 Kudos