Here's how I'm doing it on the i.MX6 in the Dora branch. The approach for the i.MX51 looks to be similar.
In the DTS, there's a group of pinmux entries named pinctrl_hog. This group is not well documented, but I've been assuming these are set up unconditionally by the kernel (pins that need to be setup, but aren't managed by any particular device/driver). Add a pinmux and pad control entry to this group along the lines of:
&iomuxc {
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_hog>;
hog {
pinctrl_hog: hoggrp {
fsl,pins = <
/* This is just an example; your own setup *will* vary. */
MX51_PAD_EIM_A27__GPIO2_21 0x5
>;
};
};
};
This example sets up the mux for pin EIM_A27 as connecting to GPIO2_21. The "argument" to the MX51_* entry is the pad control. On the i.MX6, this value is stuffed directly into the IOMUXC_SW_PAD_CTL_* register; consult your reference manual for the proper pad control values.
However, this only sets up the mux and pad control. It does not set up the GPIO state (input vs. output, current pin level, etc.). For that, it looks like you have to wait for the kernel to come up. In order to expose a GPIO to user space, you must first "export" it by writing the pin number you want exported to /sys/class/gpio/export. This in turn creates a subdirectory named /sys/class/gpio/gpio<nn>, where <nn> is the pin number. In there are various files that let you configure the GPIO pin the way you need it. However, everything under /sys is owned and writable only by root, so you (probably) need to make those entries writable by non-privileged applications.
You can do this using an init script, but I've chosen to do it using udev rules, since they run fairly early, are somewhat simpler once you understand the syntax, and will work regardless of what init system you're using. Here are the rules I created to make a GPIO entry available for applications:
ACTION=="add", SUBSYSTEM=="gpio", DEVPATH=="/devices/*/gpio/gpiochip64", ATTR{subsystem/export}="86"
ACTION=="add", SUBSYSTEM=="gpio", DEVPATH=="/devices/*/gpio/gpio86", RUN+="/bin/chgrp -R gpio $sys$devpath", RUN+="/bin/chmod -R g+w $sys$devpath"
The first rule performs the export operation, creating the entry /sys/class/gpio/gpio86. That, in turn, triggers the second rule, which changes the group ownership and permissions of everything under /sys/class/gpio/gpio86 to be writable by members of the group "gpio" (which you will need to create). You then create a user who is a member of the group "gpio", run your application(s) as that user, and you should be able to dance on the GPIO pin.
Again, this is all for the 3.10.x series kernel in Dora (and probably Daisy as well). Hope this made some sense...