Example Code: Linux-5.0.6 GPIO descriptor based Kernel Module

Post Reply
User avatar
gauravpathak
Posts: 6
Joined: Mon Oct 08, 2018 10:06 pm
languages_spoken: english
ODROIDs: Odroid C2
Location: Pune,India
Has thanked: 0
Been thanked: 1 time
Contact:

Example Code: Linux-5.0.6 GPIO descriptor based Kernel Module

Unread post by gauravpathak » Thu May 23, 2019 10:36 pm

Descriptor based GPIO toggling for Linux-5.0.6 Kernel

Hello Everyone,
I have written a simple Linux Kernel module for toggling GPIO on Odroid-C2, this module is tested for Linux-5.0.6. I am posting the code here for reference and in the hope that it should be helpful for people trying to figure out accessing GPIO in kernel space on odroid-c2.

The code is posted below:

Code: Select all

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/printk.h>
#include <linux/init.h>
#include <linux/delay.h>

#include <linux/gpio/driver.h>
#include <dt-bindings/gpio/meson-gxbb-gpio.h>

struct gpio_chip *chip;

static int chip_match_name(struct gpio_chip *chip, void *data)
{
	printk(KERN_INFO "Label: %s", chip->label);
	printk(KERN_INFO "Name: %s", chip->parent->init_name);
	printk(KERN_INFO "OF Node Full Name: %s", chip->of_node->full_name);

	return !strcmp(chip->label, data);
}

int gpio_test_init(void)
{
	int err = 0;

	printk(KERN_DEBUG "Init Called\n");

	chip = gpiochip_find("periphs-banks", chip_match_name);

	if (!chip) {
		printk(KERN_ERR "Cannot Find A GPIO Chip");
		return -ENODEV;
	}
	printk(KERN_DEBUG "Got valid GPIO Chip Total num gpios %d\n",
			chip->ngpio);

	err = chip->get(chip, GPIOX_11);
	printk(KERN_INFO "Before Setting Value %d\n", err);

	err = chip->direction_output(chip, GPIOX_11, 1);
	if (err < 0) {
		printk(KERN_DEBUG "Error Setting GPIO Direction %d", err);
	}
	chip->set(chip, GPIOX_11, 1);

	err = chip->get(chip, GPIOX_11);
	printk(KERN_INFO "After Setting Value %d\n", err);

	mdelay(2000);

	chip->set(chip, GPIOX_11, 0);

	err = chip->get(chip, GPIOX_11);
	printk(KERN_INFO "After Clearing Value %d\n", err);

	return 0;
}

void gpio_test_exit(void)
{
	printk(KERN_DEBUG "Exiting....\n");
}

module_init( gpio_test_init);
module_exit( gpio_test_exit);

MODULE_LICENSE("GPL");
Makefile to compile the above Kernel Module:

Code: Select all

obj-m += gpio_test.o

KSRC = </path/to/pre-compiled/kernel-source>

EXTRA_CFLAGS = -I$(KSRC)/drivers/pinctrl/meson
EXTRA_CFLAGS += -I$(KSRC)/drivers/

CFLAGS_gpio_test.o := -DDEBUG

all:
	make -C $(KSRC) M=$(PWD) modules
clean:
	make -C $(KSRC) M=$(PWD) clean
Make command to compile the module:

Code: Select all

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
Make command to Clean the module:

Code: Select all

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- clean
I am currently looking into accessing the GPIO interrupts, I will post the code when I get it working. :-)

User avatar
odroid
Site Admin
Posts: 31377
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Has thanked: 52 times
Been thanked: 184 times
Contact:

Re: Example Code: Linux-5.0.6 GPIO descriptor based Kernel Module

Unread post by odroid » Fri May 24, 2019 9:17 am

Nice to see a working GPIO example for a modern Kernel 5.0.

Can you tell me which Kernel source tree do you use?

User avatar
gauravpathak
Posts: 6
Joined: Mon Oct 08, 2018 10:06 pm
languages_spoken: english
ODROIDs: Odroid C2
Location: Pune,India
Has thanked: 0
Been thanked: 1 time
Contact:

Re: Example Code: Linux-5.0.6 GPIO descriptor based Kernel Module

Unread post by gauravpathak » Fri May 24, 2019 2:57 pm

Hi @odroid,
I took the Linux kernel from git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git and switched to branch linux-5.0.y.
I applied few patches to this kernel source tree. You can find those patches in attachment and also a defconfig file for this Kernel Source.
The only thing is that USB hot-plugging doesn't seem to work, any usb device needs to be attached prior to booting the board in order for it to work.

One more thing I would like to mention that the above GPIO module code depends on PINCTRL_MESON_GXBB driver, so one needs to enable it in the Linux source tree.

Code: Select all

Symbol: PINCTRL_MESON_GXBB [=y]
Type  : bool
Prompt: Meson gxbb SoC pinctrl driver
Location:  
	-> Device Drivers
		-> Pin controllers (PINCTRL [=y])
			-> Amlogic SoC pinctrl drivers (PINCTRL_MESON [=y])
odroid-5.0-patches.zip
Patches and defconfig of Linux-5.0.6 for Odroid-C2
(34.24 KiB) Downloaded 12 times

User avatar
odroid
Site Admin
Posts: 31377
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Has thanked: 52 times
Been thanked: 184 times
Contact:

Re: Example Code: Linux-5.0.6 GPIO descriptor based Kernel Module

Unread post by odroid » Fri May 24, 2019 3:21 pm

Thank you for sharing the kernel patch and configuration files. :)
Are you running 5.0.18 now? Is there HDMI output too?

User avatar
gauravpathak
Posts: 6
Joined: Mon Oct 08, 2018 10:06 pm
languages_spoken: english
ODROIDs: Odroid C2
Location: Pune,India
Has thanked: 0
Been thanked: 1 time
Contact:

Re: Example Code: Linux-5.0.6 GPIO descriptor based Kernel Module

Unread post by gauravpathak » Fri May 24, 2019 3:38 pm

Yes, I am currently running 5.0.18.
The HDMI is also working. I built core-image-sato using yocto for HDMI to work.

If you need configuration files and procedure for building the image using yocto, please let me know.

User avatar
odroid
Site Admin
Posts: 31377
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Has thanked: 52 times
Been thanked: 184 times
Contact:

Re: Example Code: Linux-5.0.6 GPIO descriptor based Kernel Module

Unread post by odroid » Fri May 24, 2019 5:02 pm

Glad to know that.
Thank you for the confirmation.

Can you write an article for our ODROID Magazine? Many users will be interested in Running Yocto on the ODROID-C2 with a modern Kernel 5.0 :D
If yes, our chief editor will contact you early next week.
Please consider this proposal positively.

outhudd9
Posts: 37
Joined: Wed May 02, 2018 10:52 pm
languages_spoken: english
Has thanked: 0
Been thanked: 0
Contact:

Re: Example Code: Linux-5.0.6 GPIO descriptor based Kernel Module

Unread post by outhudd9 » Fri May 24, 2019 5:49 pm

It would be great to read and very useful!

User avatar
gauravpathak
Posts: 6
Joined: Mon Oct 08, 2018 10:06 pm
languages_spoken: english
ODROIDs: Odroid C2
Location: Pune,India
Has thanked: 0
Been thanked: 1 time
Contact:

Re: Example Code: Linux-5.0.6 GPIO descriptor based Kernel Module

Unread post by gauravpathak » Fri May 24, 2019 6:23 pm

Yeah, I am glad to write an Article for the Magazine. Thanks for offering the opportunity to me.
I also need to go through all the setup steps, as I did some tweaks for some errors that I encountered during yocto build.
These users thanked the author gauravpathak for the post:
joy (Fri May 31, 2019 5:52 pm)

User avatar
odroid
Site Admin
Posts: 31377
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID
Has thanked: 52 times
Been thanked: 184 times
Contact:

Re: Example Code: Linux-5.0.6 GPIO descriptor based Kernel Module

Unread post by odroid » Fri May 24, 2019 6:47 pm

Really appreciate it. :D

Post Reply

Return to “Projects”

Who is online

Users browsing this forum: No registered users and 0 guests